0
votes

I am getting a type mismatch with the following syntax in my Access VBA. I am trying to update my table named "Billing" by seeing if any records have a date that looks at a string value in my "Certs" table like "2012-07-01" corresponding to my form's billYear textbox e.g. 2012 and my billMonth textbox e.g. 07. Is there a better way to write the VBA or see an error - many thanks:

Dim sRecert As String
Dim byear As Integer
Dim bmonth As Integer

byear = Me.billYear
bmonth = Me.billMonth

sRecert = "Update Billing set recertifying = -1 where (select     certificationExpireDate from certs where Left((certificationExpireDate),4) =  " & byear
    & " and Mid((certificationExpireDate),6,2) =  " & bmonth & ")"

DoCmd.RunSQL sRecert

I may not have explained it well. I created a real Query called from my form: DoCmd.OpenQuery "updateRecert"
I set up my SQL below as a test on a real date I’m working with. It is in SQL Server (ODBC linked) My dbo_certs table and my dbo_billing table share only one joinable field peopleID:

UPDATE dbo_Billing AS a INNER JOIN dbo_certs AS b ON a.peopleid = b.peopleid 
SET a.recertifying = -1
WHERE b.certificationExpireDate = '2015-08-31 00:00:00.000';

The above gave a data mismatch error. My bottom line is I have two text boxes on my form to pass in data preferably into my VBA code:

  • billMonth which in this case is 8 because it is an integer so that is a problem
  • billYear is 2015

so I need to update my dbo_billing table’s ‘recertifying’ field with -1 if the dbo_cert’s field ‘certificationExpireDate’ is '2015-08-31 00:00:00.000' but only if that can be gotten from the form.

2

2 Answers

1
votes

Is there a better way to write the VBA or see an Error?

Yes. You need Error Handling

I don't think the issue is in the code, I think it's in the SQL.

To troubleshoot your code, wrap it in an good error handler.

Public Sub MyRoutine()

    On Error GoTo EH

    'put your code here

    GoTo FINISH

EH:

    With Err
        MsgBox "Error" & vbTab & .Number & vbCrLf _
            & "Source" & vbTab & .Source & vbCrLf & vbCrLf _
            & .Description
    End With

    'for use during debugging
    Debug.Assert 0
    GoTo FINISH
    Resume

FINISH:

    'any cleanup code here

End Sub

When the msgbox shows the error, make note of the Source. This should help you determine where the error comes from.

The lines following 'for use during debugging are helpful. Here's how to use them:

  • execution will stop on the Debug.Assert 0 line
  • drag the yellow arrow (which determines which line to run next) to the Resume line
  • hit {F8} on the keyboard (or use the menu Debug > Step Into)

This will go to the line where the error occurred. In your case, it will probably be the last line of your code.

0
votes

Error in SQL... but!! Are you sure that certificationExpireDate is string and all the time equal to yyyy-mm-dd pattern?? It's dangerouse to have relation with "not certain" key like you have. I think this is not a good db design.

But, after all, for your case:

VBA:

sRecert = "UPDATE Billing a inner join certs b " & _
          "on format(a.imaginary_date_field, """yyyy-mm-dd""") = b.certificationExpireDate " & _
          "set a.recertifying = -1 " & _
          "where CInt(Left((b.certificationExpireDate),4)) =  " & byear & " and CInt(Mid((b.certificationExpireDate),6,2)) =  " & bmonth

QueryDef:

PARAMETERS Forms!your_form!byear Short, Forms!your_form!bmonth Short;
UPDATE Billing a inner join certs b
    on format(a.imaginary_date_field, "yyyy-mm-dd") = b.certificationExpireDate
    set a.recertifying = -1
    where CInt(Left((b.certificationExpireDate),4)) = Forms!your_form!byear and CInt(Mid((b.certificationExpireDate),6,2)) = Forms!your_form!bmonth

UPDATED

mismatch error

You get error probable because you have date/time field, not a string. Date in MS Access queries write with # symbol. WHERE b.certificationExpireDate = #2015-08-31 00:00:00.000#;

In your case:

PARAMETERS Forms!your_form!byear Short, Forms!your_form!bmonth Short;
UPDATE dbo_Billing AS a INNER JOIN dbo_certs AS b ON a.peopleid = b.peopleid 
SET a.recertifying = -1
WHERE year(b.certificationExpireDate) = Forms!your_form!byear and Month(b.certificationExpireDate) = Forms!your_form!bmonth;

For more info follow this link