Using RecordSets with variables (string issue)

maxhavoc

Registered User.
Local time
Yesterday, 23:34
Joined
Dec 12, 2005
Messages
14
I'm trying to use a RecordSet to pull data out of an Access DB. I need to compare a field in the DB to a text box in my form. This is what I have so far.

Code:
Dim Hardware As Database
Dim Records As Recordset
Dim strSQL As String
Set Hardware = OpenDatabase("c:\hardware.mdb")
Serial.SetFocus
strSQL = "SELECT * FROM Computers WHERE Computers.Serial = " & Serial.Text

Set Records = Hardware.OpenRecordset(strSQL)

When I try to run this I get a data type mismatch. Some explanation.
Computers is a table, Computers.Serial is a field in that table. The "Serial" in Serial.Text is a field in my form. You may ask why I'm using the "&" operator instead of just putting it all in the string. The reason is that I cannot figure out how to get VB to treat "Serial.text" as a variable and not a string literal. When I try to put it in single quotes (') I get an error saying the OpenRecordSet call requires an argument, and I'm not providing one, meaning I'm quoting it wrong somehow. Any ideas?
 
strSQL = "SELECT * FROM Computers WHERE Computers.Serial = '" & Serial.Text & "'"

Also, are you sure you want to use the .Text property? This captures any uncommited value, and could be null. I'm thinking you really want to use just Serial or Serial.value.
 
Last edited:
Oh thank you! Finally I get that damned thing working! As for your other comment, I check the input before doing anything with it, so that's not a concern, I'm not new to programming, just to VB so I know how to validate input, but thanks for the suggestion and the help.
 

Users who are viewing this thread

Back
Top Bottom