Hello, I'm struggling up the steep VB learning curve and trying to write code that will "read" the contents of a cell in a form so that I can search another table for the same string. Hoping someone can read what I have and point out what I'm doing wrong.
The user uses the form to enter data. The code runs after they have entered an ID. My objective is to display a warning if what the user enters isn't in the 'other' table.
This is what I have so far - specific problems are embedded and bold:
Private Sub PointID_LostFocus()
Dim p As Integer
Dim currPt As String
currPt = "=[Forms]![Point_Count_DataEntry]![PointID]"
' this is supposed to read what the user just entered in the form
' Problem 1: I put a watch on currPt and the value doesn't change
' I would expect it to list exactly what the user just entered but it
' doesn't.
DoCmd.OpenTable "Points", acNormal, acReadOnly
DoCmd.GoToControl "PointID"
' open the 'other' table and go to the PointID field - this is working
p = DCount(currPt, "Points")
'supposed to count the number of records with the same PointID the user
' just entered. I also tried
![Points]![PointID] in place of "Points"
' both with and without quotes - all ways I get various syntax errors.
'Problem 2: I get a runtime error (3075) and it can't get past this line.
' it says there is a syntax error (missing operator) in query
' expression: 'Count(=[Forms...PointID])'. So refers back to the code ' in currPt. Maybe if I can fix that line this one will work...?
If p = 0 Then
Response = MsgBox("Please check the PointID. The ID you entered does not exist", , "Warning")
Else: DoCmd.Close acTable, "Points", acSaveNo
End If
End Sub
The user uses the form to enter data. The code runs after they have entered an ID. My objective is to display a warning if what the user enters isn't in the 'other' table.
This is what I have so far - specific problems are embedded and bold:
Private Sub PointID_LostFocus()
Dim p As Integer
Dim currPt As String
currPt = "=[Forms]![Point_Count_DataEntry]![PointID]"
' this is supposed to read what the user just entered in the form
' Problem 1: I put a watch on currPt and the value doesn't change
' I would expect it to list exactly what the user just entered but it
' doesn't.
DoCmd.OpenTable "Points", acNormal, acReadOnly
DoCmd.GoToControl "PointID"
' open the 'other' table and go to the PointID field - this is working
p = DCount(currPt, "Points")
'supposed to count the number of records with the same PointID the user
' just entered. I also tried
' both with and without quotes - all ways I get various syntax errors.
'Problem 2: I get a runtime error (3075) and it can't get past this line.
' it says there is a syntax error (missing operator) in query
' expression: 'Count(=[Forms...PointID])'. So refers back to the code ' in currPt. Maybe if I can fix that line this one will work...?
If p = 0 Then
Response = MsgBox("Please check the PointID. The ID you entered does not exist", , "Warning")
Else: DoCmd.Close acTable, "Points", acSaveNo
End If
End Sub