Hi all ,
I am struggling with a set of VBA coding that a previous person on the project was using. I am fixing their mess and i cannot for nuts figure out why the error comes up. So let me describe what i am trying here.
There is a parent table with basic information about each vendor. I have linked the parent table with a bunch of other tables including the one that stores contacts for each vendor. There is a main form that has the contacts in a subform. The subform has a list box that has to list the name and role of each person for that vendor.
This is my code
Now my error "Run time error '3075': Syntax error (missing operator) in query expression 'ContactID = Baker Tom'. pops up and the line that is highlighted is in the List box on click Sub as shown below.(underlined in code)
Can anyone please help me. I am tired of staring at the code and not knowing what to do. You can see how the subform in the attachment.
I am struggling with a set of VBA coding that a previous person on the project was using. I am fixing their mess and i cannot for nuts figure out why the error comes up. So let me describe what i am trying here.
There is a parent table with basic information about each vendor. I have linked the parent table with a bunch of other tables including the one that stores contacts for each vendor. There is a main form that has the contacts in a subform. The subform has a list box that has to list the name and role of each person for that vendor.
This is my code
Code:
' Adding new vendor
Private Sub AddCont_Click()
Me.author.Value = ""
Me.AuthorRole.Value = ""
Me.RoleOth.Value = ""
Me.Condition.Value = ""
Me.othrcondition.Value = ""
Me.CmbTest.Value = ""
Me.email.Value = ""
Me.phone.Value = ""
Me.Fax.Value = ""
Me.fix.Value = ""
Me.chkCurrent.Value = "False"
Me.txtAction.Value = "NewRecord"
End Sub
'Save command button on Subform
Private Sub Command5_Click()
Dim db As Database
Set db = CurrentDb
Dim rs As DAO.Recordset
Dim SQLStr As String
Dim SQLstr2 As String
On Error GoTo HandleError
If Me.txtAction.Value = "NewRecord" Then
Set rs = CurrentDb.OpenRecordset("Contacts")
With rs
.AddNew
![siteid] = Me.Parent!NewSiteID.Value
![Name] = Me.author.Value
![Role] = Me.AuthorRole.Value
![ORole] = Me.RoleOth.Value
![Speciality] = Me.Condition.Value
![SpecialityOther] = Me.othrcondition.Value
![Test] = Me.CmbTest.Value
![email] = Me.email.Value
![phone] = Me.phone.Value
![Fax] = Me.Fax.Value
![Suffix] = Me.fix.Value
![IsCurrent] = Me.chkCurrent.Value
.Update
End With
rs.Close
Set rs = Nothing
MsgBox ("Record is saved")
Me.txtAction.Value = ""
Call RefreshLstAuthors
'refresh list box
ElseIf Me.txtAction.Value = "UpdateRecord" Then
SQLStr = "UPDATE Contacts SET Name = " & "'" & Me.author.Value & "'" & ", Role = " & "'" & Me.LstAuthors.Value & "'" & _
", ORole = " & "'" & RoleOth & "'" & ", Speciality = " & "'" & Me.Condition.Value & "'" & _
", SpecialityOther = " & "'" & Me.othrcondition.Value & "'" & ", Test = " & "'" & Me.CmbTest.Value & "'" & _
", Email = " & "'" & Me.email.Value & "'" & ", Phone = " & "'" & Me.phone.Value & "'" & ", Fax = " & "'" & Me.Fax.Value & "'" & _
", Suffix = " & "'" & Me.fix.Value & "'" & ", IsCurrent = " & Me.chkCurrent.Value & _
" WHERE ContactID = " & Me.LstAuthors.Value & " AND SiteID = " & Me.Parent!NewSiteID.Value
db.Execute SQLStr
Call RefreshLstAuthors
MsgBox ("Record is updated")
Else
End If
db.Close
ExitHere:
Exit Sub
HandleError:
MsgBox Err.Description
Resume ExitHere
End Sub
'On load - subform
Private Sub Form_Load()
Me.author.Value = ""
Me.AuthorRole.Value = ""
Me.RoleOth.Value = ""
Me.Condition.Value = ""
Me.othrcondition.Value = ""
Me.CmbTest.Value = ""
Me.email.Value = ""
Me.phone.Value = ""
Me.Fax.Value = ""
Me.fix.Value = ""
Me.chkCurrent.Value = ""
Me.txtAction.Value = ""
End Sub
'On Open - Subform
Private Sub Form_Open(Cancel As Integer)
Me.author.Value = ""
Me.AuthorRole.Value = ""
Me.RoleOth.Value = ""
Me.Condition.Value = ""
Me.othrcondition.Value = ""
Me.CmbTest.Value = ""
Me.email.Value = ""
Me.phone.Value = ""
Me.Fax.Value = ""
Me.fix.Value = ""
Me.chkCurrent.Value = ""
Me.txtAction.Value = ""
Dim db As DAO.Database
Set db = CurrentDb
Dim rcount As DAO.Recordset
Dim rs As DAO.Recordset
Dim SQLCount As String
Dim SQLStr As String
Dim Records As String
Dim Count As Integer
Dim i As Integer
Dim rsDate As String
Dim rsAuthor As String
Dim rsNote As String
Dim strReturn As String
SQLCount = "SELECT Count(*) FROM Contacts WHERE Contacts.SiteID = " & Me.Parent!NewSiteID.Value
Set rcount = db.OpenRecordset(SQLCount, dbOpenDynaset)
rcount.MoveFirst
Count = rcount.Fields(0).Value
rcount.Close
Set rcount = Nothing
' recordset
SQLStr = "SELECT Contacts.ContactID, Contacts.Name, Contacts.Role, Contacts.ORole" & _
", Contacts.Speciality, Contacts.SpecialityOther ,Contacts.Email, Contacts.Phone, Contacts.Fax, Contacts.Test " & _
", Contacts.IsCurrent, Contacts.Suffix " & _
" FROM Contacts WHERE Contacts.SiteID = " & Me.Parent!NewSiteID.Value
Set rs = db.OpenRecordset(SQLStr, dbOpenDynaset)
Me.LstAuthors.RowSourceType = "Value List"
If Count = 0 Then
Me.LstAuthors.RowSource = ""
Else
rs.MoveFirst
Do Until rs.EOF
For i = 0 To Count - 1
With Me.LstAuthors
.RowSourceType = "Value List"
.ColumnCount = 4
.AddItem Item:=rs![ContactID] & ";" & rs![Name] & ";" & rs![Role] & ";" & rs![ORole], Index:=i
End With
rs.MoveNext
Next i
Loop
End If
rs.Close
Set rs = Nothing
db.Close
End Sub
'Refreshing List box
Private Sub RefreshLstAuthors()
Me.LstAuthors.RowSource = ""
Me.author.Value = ""
Me.AuthorRole.Value = ""
Me.RoleOth.Value = ""
Me.Condition.Value = ""
Me.othrcondition.Value = ""
Me.CmbTest.Value = ""
Me.email.Value = ""
Me.phone.Value = ""
Me.Fax.Value = ""
Me.fix.Value = ""
Me.chkCurrent.Value = ""
Me.txtAction.Value = ""
Dim db As DAO.Database
Dim rcount As DAO.Recordset
Dim rs As DAO.Recordset
Dim SQLStr As String
Dim SQLCount As String
Dim Records As String
Dim Count As Integer
Dim i As Integer
SQLCount = "SELECT Count(*) FROM Contacts WHERE Contacts.SiteID = " & Me.Parent!NewSiteID.Value
Set db = CurrentDb
Set rcount = db.OpenRecordset(SQLCount, dbOpenDynaset)
rcount.MoveFirst
Count = rcount.Fields(0).Value
rcount.Close
Set rcount = Nothing
SQLStr = "SELECT Contacts.ContactID, Contacts.Name, Contacts.Role, Contacts.ORole" & _
", Contacts.Speciality, Contacts.SpecialityOther ,Contacts.Email, Contacts.Phone, Contacts.Fax, Contacts.Test " & _
", Contacts.IsCurrent, Contacts.Suffix " & _
"FROM Contacts WHERE SiteID = " & Me.Parent!NewSiteID.Value
Set rs = db.OpenRecordset(SQLStr, dbOpenDynaset)
Me.LstAuthors.RowSourceType = "Value List"
If Count < 0 Then
Else
rs.MoveFirst
Do Until rs.EOF
For i = 0 To Count - 1
With Me.LstAuthors
.RowSourceType = "Value List"
.ColumnCount = 4
.AddItem Item:=rs![ContactID] & ";" & rs![Name] & ";" & rs![Role] & ";" & rs![ORole]
End With
rs.MoveNext
Next i
Loop
End If
rs.Close
Set rs = Nothing
db.Close
End Sub
' List box on click
Private Sub LstAuthors_Click()
Dim SQLStr As String
Dim db2 As Database
Dim rst As DAO.Recordset
Set db2 = CurrentDb
If IsNull(Me.LstAuthors.Value) = False Then
SQLStr = "SELECT Name, Role, ORole, Speciality, SpecialityOther, Email, Phone, Fax, Test, IsCurrent, Suffix FROM Contacts WHERE ContactID = " & Me.LstAuthors.Value
Set rst = db2.OpenRecordset(SQLStr, dbOpenDynaset)
rst.MoveFirst
Me.author.Value = rst.Fields(0).Value
Me.AuthorRole.Value = rst.Fields(1).Value
Me.RoleOth.Value = rst.Fields(2).Value
rst.Close
db.Close
Me.txtAction.Value = "UpdateRecord"
Else
End If
End Sub
Now my error "Run time error '3075': Syntax error (missing operator) in query expression 'ContactID = Baker Tom'. pops up and the line that is highlighted is in the List box on click Sub as shown below.(underlined in code)
Code:
' List box on click
Private Sub LstAuthors_Click()
Dim SQLStr As String
Dim db2 As Database
Dim rst As DAO.Recordset
Set db2 = CurrentDb
If IsNull(Me.LstAuthors.Value) = False Then
SQLStr = "SELECT Name, Role, ORole, Speciality, SpecialityOther, Email, Phone, Fax, Test, IsCurrent, Suffix FROM Contacts WHERE ContactID = " & Me.LstAuthors.Value
[U] Set rst = db2.OpenRecordset(SQLStr, dbOpenDynaset)[/U]
rst.MoveFirst
Me.author.Value = rst.Fields(0).Value
Me.AuthorRole.Value = rst.Fields(1).Value
Me.RoleOth.Value = rst.Fields(2).Value
rst.Close
db.Close
Me.txtAction.Value = "UpdateRecord"
Else
End If
End Sub
Can anyone please help me. I am tired of staring at the code and not knowing what to do. You can see how the subform in the attachment.