looking to see if there are any records in the table"tblpipeLineInput" with an ID equal to that contained in a textbox "txtStationID" (1 Viewer)

Bobp3114

Member
Local time
Today, 16:24
Joined
Nov 11, 2020
Messages
83
My code:
Private Sub cboOpportunity_GotFocus()
Dim rst As DAO.Recordset

'Get the database and Recordset
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("tblPipeLineInput")

'Search for the first matching record
rst.FindFirst "[StationID] = txtStationID"

'Check the result
If rst.NoMatch Then
MsgBox "Record not found."

Else
Exit Sub
End If

rst.Close
Set rst = Nothing
Set dbs = Nothing

End Sub

Result:
1767315954539.png


Can somebody explain where I am going wrong? As I have tried all sorts of alternatives. I am simply looking to see if there are any records in the table"tblpipeLineInput" with an ID equal to that contained in a textbox "txtStationID"
Thanks
 
You are trying to build a literal string with variable parts. You can't just cram the variable inside quote marks and hope the computer knows what you are talking about.

"This is a literal string because it is all inside quote marks. This is the value inside YourVariable: YourVariable"

The computer sees the above as it literally appears, it sees the text "YourVariable" twice, it doesn't interpret the second one as the value inside your variable because the computer doesn't interpret English, it just spits out what you told it to. If you want the actual value of YourVariable to appear at the end of that sentence you must do this:

"This is a literal string because it is all inside quote marks. This is the value inside YourVariable: " & YourVariable

Variable values exist outside of quote marks.
 
Me again, you did a few things wrong, so I need to add more:

1. To reference a control on a form (which is a variable) you do it like this:

Me.ControlName

2. Building that string you want is even more difficult when its ultimately used in a string comparison. You must surround the value in that variable with single quotes inside the double quotes:

"Literal string here. And YourVariable has the string value of: '" & YourVariable & "'"

If you look closely there are single quotes inside the double quotes just before and after I inserted YourVariable value into the string. So, let's say YourVariable contains "Plog Rules". The above string would become:

Literal string here. And YourVariable has the string value of: 'Plog Rules'

So, you need to change:

rst.FindFirst "[StationID] = txtStationID"

to (if StationID is text)

rst.FindFirst "[StationID] = '" & Me.txtStationID & "'"

to (if StationID is numeric)

rst.FindFirst "[StationID] = " & Me.txtStationID
 
Why not :
SQL:
If DCount ("*" , "tblPipeLineInput" , "StationID = " & txtStationID) = 0 then
    MsgBox "Record not found."
End if

You don't even need the ELSE section. Because you do nothing there.
 

Users who are viewing this thread

  • Back
    Top Bottom