• Welcome, Guest. Please login.
 
September 24, 2020, 09:40:48 am

News:

Welcome to the SQLitening support forums!


Problem executing a query

Started by jonas, October 09, 2008, 01:50:38 pm

Previous topic - Next topic

jonas

I have a problem creating a query in VB6. First time I run the code:

x = SQL.slOpen("CMMS.db3", "E0")
    y = SQL.slSel("SELECT * FROM users WHERE UserName = """ + Trim(EditName.Text) + """", Record)
    If SQL.slGetRow(Record, "E0") Then
        UserInfo.Name = slFNX("UserName", Record)
        UserInfo.Engineer = slFNX("Engineer", Record)
        UserInfo.Type = slFNX("Type", Record)
        UserInfo.Password = slFNX("Password", Record)
    End If


it returns nothing, but second time it returns the right record. If I use
x = SQL.slSel("SELECT * FROM users", Record, "E0")
it gives me all the records every time (I mean, it always works), how can I search for a specific record?

Thanks
Jonas

Paul Squires

I don't have VB handy but the WHERE portion of your SELECT looks wrong. You need to surround your search string with single quotes instead of the double quotes that you are using (also, you have used """"  <-- notice 4 instead of 3 double qoutes for your right side quote marker).

Try this:

y = SQL.slSel("SELECT * FROM users WHERE UserName = "'" + Trim(EditName.Text) + "'", Record)


Matt Humphreys

October 09, 2008, 11:33:58 pm #2 Last Edit: October 10, 2008, 04:22:00 am by matt humphreys
'One more tweak ...
'Note the first "'" becomes '"
y = SQL.slSel("SELECT * FROM users WHERE UserName = '" + Trim(EditName.Text) + "'", Record)

'In Vb one could also declare the search term as a string variable
Dim sEditName as string
'force a string concatenation with a zero length string [using ampersand (&)] to get rid of any nulls
sEditname = EditName.Text & ""
'now because we have a true string then use Trim$ instead of Trim
sEditname = Trim$(sEditname)
'and again use the ampersand (&) to concatenate
y = SQL.slSel("SELECT * FROM users WHERE UserName = '" & sEditName & "'", Record)
'or use the Chr function for the last single quote
y = SQL.slSel("SELECT * FROM users WHERE UserName = '" & sEditName & Chr$(39), Record)

jonas

Thank you very much for your help, the ampersand by itself solved the problem.