Solved Single Quotes in Criteria

JithuAccess

Member
Local time
Today, 03:57
Joined
Mar 3, 2020
Messages
325
Hello Guys,

I want to display File Name from my Table tblFileBasic in to a Text in my Form frmFileRequest if the File Number in the text box of the Form matches the File Number in the Table.

This is my Code:

Code:
Dim db As Database
Dim rst As Recordset
Dim strsql As String

strsql = "Select File_Name from tblFileBasic where (tblFileBasic.File_Number = Forms!frmFileRequest!File_Number)"

Set db = CurrentDb
Set rst = db.OpenRecordset(strsql, dbOpenDynaset)

Text15 = rst.Fields(0)

I am getting Error Message like "Too Few Parameters.."

I think I should add a single quotes in where but I am not so sure how to add this. I tried Ampersand but it's not working.

Thanks
 
You need to concatenate the external value in to your SQL string:
Code:
' ...
strsql = "Select File_Name from tblFileBasic where tblFileBasic.File_Number = " & Forms!frmFileRequest!File_Number
' ...
 
If File_Number is a string datatype the, yes, you also need to add quotes:
Code:
' ...
strsql = "Select File_Name from tblFileBasic where tblFileBasic.File_Number = '" & Forms!frmFileRequest!File_Number & "'"
'                                                                             ^                                     ^^^^^
'                                                                             |                                     |||||
' ...
 
If File_Number is a string datatype the, yes, you also need to add quotes:
Code:
' ...
strsql = "Select File_Name from tblFileBasic where tblFileBasic.File_Number = '" & Forms!frmFileRequest!File_Number & "'"
'                                                                             ^                                     ^^^^^
'                                                                             |                                     |||||
' ...


Thank you so much. It works
 
I want to display File Name from my Table tblFileBasic in to a Text in my Form

Cheeky's post is a good lesson in how to build strings in VBA, but the method you have chosen to achieve the above objective is overkill. You can achieve the same thing with a DLookup as the control source of that text box.
 

Users who are viewing this thread

Back
Top Bottom