error is in the line db.Execute strSQL, dbFailOnError.

azhar2006

Registered User.
Local time
Today, 14:21
Joined
Feb 8, 2012
Messages
297
Hello guys
I want to execute this query with a button that runs the code in (vba) but I get this error at line db.Execute strSQL, dbFailOnError.
Any help, thanks everyone
 

Attachments

Your strSQL has the number as a string so you wil get a datatype mismatch when trying to run the query.

UPDATE Table2 SET Table2.Specialization = 'E' WHERE ((Table2.UnifiedNumber)='681234299')
 
Your strSQL has the number as a string so you wil get a datatype mismatch when trying to run the query.

UPDATE Table2 SET Table2.Specialization = 'E' WHERE ((Table2.UnifiedNumber)='681234299')
Yes, I know, I want him to cross this error
 
'him' has already given the information to 'cross the error'. What is it that you don't understand?
 
unifiedNumber is Number not string.
also there are Blank/Null specialization so change
the variable strSpecialization to Variant:
Code:
Private Sub Command0_Click()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strUnifiedNumber As Long
Dim strSpecialization As Variant
Dim strSQL As String
Set db = CurrentDb
Set rs = db.OpenRecordset("qryUnifiedNumber", dbOpenSnapshot)
'Make sure we have records and then
'make sure we are at the first record
If rs.RecordCount < 1 Then
    MsgBox "No records require an update"
    Set rs = Nothing
    Set db = Nothing
    Exit Sub
End If
rs.MoveFirst
'We need to loop through all of the records
'that our query object found
While rs.EOF = False

    strUnifiedNumber = rs![UnifiedNumber]
    strSpecialization = rs![Specialization]
    strSQL = "UPDATE Table2 SET Table2.Specialization = '" & strSpecialization & "' WHERE ((Table2.UnifiedNumber)=" & strUnifiedNumber & ")"

    db.Execute strSQL, dbFailOnError

rs.MoveNext

Wend
rs.Close
Set rs = Nothing
Set db = Nothing
MsgBox "Complete"
End Sub
 
unifiedNumber is Number not string.
also there are Blank/Null specialization so change
the variable strSpecialization to Variant:
Code:
Private Sub Command0_Click()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strUnifiedNumber As Long
Dim strSpecialization As Variant
Dim strSQL As String
Set db = CurrentDb
Set rs = db.OpenRecordset("qryUnifiedNumber", dbOpenSnapshot)
'Make sure we have records and then
'make sure we are at the first record
If rs.RecordCount < 1 Then
    MsgBox "No records require an update"
    Set rs = Nothing
    Set db = Nothing
    Exit Sub
End If
rs.MoveFirst
'We need to loop through all of the records
'that our query object found
While rs.EOF = False

    strUnifiedNumber = rs![UnifiedNumber]
    strSpecialization = rs![Specialization]
    strSQL = "UPDATE Table2 SET Table2.Specialization = '" & strSpecialization & "' WHERE ((Table2.UnifiedNumber)=" & strUnifiedNumber & ")"

    db.Execute strSQL, dbFailOnError

rs.MoveNext

Wend
rs.Close
Set rs = Nothing
Set db = Nothing
MsgBox "Complete"
End Sub
Good evening arnelgp, thank you very much. The code worked without errors, but it did not update all records. Note the attached images
 

Attachments

  • 1.JPG
    1.JPG
    41.9 KB · Views: 190
  • 2.JPG
    2.JPG
    38.7 KB · Views: 167
  • arnelgp.accdb
    arnelgp.accdb
    516 KB · Views: 208

Users who are viewing this thread

Back
Top Bottom