Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem passing the value of a text box into sql select statement

Status
Not open for further replies.

EdRev

Programmer
Aug 29, 2000
510
US
I'm new to Access programming and I hope somebody can help me with this code. I'm trying to query a table based on user's input.

Private Sub cmdAdd_Click()
Dim db As Database
Dim recordsource
Dim sqltxt As String
Dim areano As String
areano = Trim(txtAreaNo.Value)

sqltxt = "SELECT * from area_address where area_no = [areano] ;"
Set db = CurrentDb()

Set recordsource = db.OpenRecordset(sqltxt)

If Not (recordsource.EOF) Then
MsgBox ("Area already exist")
Else
frmAddArea.show
End If

End Sub

The sql doesn't work and when I change the [areano] into a string value, the else part of my if-else statement gave me a compilation error.

Any help will be greatly appreciated.

 
If areano is a number format then you should surround it with #
If it's a string format you should surround it with '

try:
sqltxt = "SELECT * FROM area_address WHERE area_no = " & "#" &areano & "#"
or:
sqltxt = "SELECT * FROM area_address WHERE area_no = '" & areano & "'"

hope this helps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top