Solved

strSQL with multiple criteria problem

Posted on 2011-09-14
12
791 Views
Last Modified: 2012-05-12
I have a string with multiple and cannot get it to work.  It will work with any two of the criteria but not the third.  I have tried so many different ways.  What am I missing?

Dim sLoadLoc as String
Dim sLoadNum as Variant
Dim sLoadLine as Variant

    sLoadNum = Forms![frm_bol_hed]![txt_loadNum]
    sLoadLoc = Forms![frm_bol_hed]![txt_loc]
    sLineNum = Me.LD_DET_LINE

            strSQL = "select * from TBL_LOAD_DET where [LD_DET_LOC] = '" & sLoadLoc & "' And [LD_DET_LINE] =" & sLineNum And [LD_DET_NUM] = " & sLoadNum"
        Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)
0
Comment
Question by:pskeens
  • 6
  • 5
12 Comments
 
LVL 75
ID: 36540461

Try this:



            strSQL = "select * from TBL_LOAD_DET where [LD_DET_LOC] = " & Chr(34) & sLoadLoc &  Chr(34) & " And [LD_DET_LINE] =" & Chr(34) & sLineNum & Chr(34) & " And [LD_DET_NUM] = " & Chr(34) & sLoadNum & Chr(34)
        Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)

0
 
LVL 75
ID: 36540466
btw ... Chr(34) is a Double Quote

0
 
LVL 2

Author Comment

by:pskeens
ID: 36540503
Negative

"Datatype mismatch in expression"

Same error I was getting before.
0
 
LVL 2

Author Comment

by:pskeens
ID: 36540508
strLoadNUm and strLineNum are numeric types.  These should not be in double quotes, correct?
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36540510
You should add the line "Option Explicit" to the top of any code module, right under Option Compare Database.  Doing so forces you to explicitly declare any variables used.

What data type is sLineNum?  numeric or text?  You don't have a Dim statement for it.

When writing SQL statements, you need to delimit text fields with quotes, dates with # marks, and numeric fields do not get delimited.

mx's code above assumes that all fields are text.
0
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Access MVP) earned 500 total points
ID: 36540523
"strLoadNUm and strLineNum are numeric types.  "
Then it would help if the naming convention was not implying String ... So ...


            strSQL = "select * from TBL_LOAD_DET where [LD_DET_LOC] = " & Chr(34) & sLoadLoc &  Chr(34) & " And [LD_DET_LINE] =" &  sLineNum  & " And [LD_DET_NUM] = " & sLoadNum  
        Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 75
ID: 36540536
In fact, you have

Dim sLoadLoc as String
Dim sLoadNum as Variant
Dim sLoadLine as Variant

Then


    sLoadNum = Forms![frm_bol_hed]![txt_loadNum]
    sLoadLoc = Forms![frm_bol_hed]![txt_loc]
    sLineNum = Me.LD_DET_LINE

So ... because of the 's' prefix ... seems to imply String.

mx
0
 
LVL 2

Author Closing Comment

by:pskeens
ID: 36540538
Winner winner, chicken dinner!  You the man!  I see what I missed now.  Stupid ampersands!!!  haha.

Thanks
0
 
LVL 75
ID: 36540551
Can we make that fish, as I am a Fishetarian.

And how about we work on those naming conventions :-)

mx
0
 
LVL 2

Author Comment

by:pskeens
ID: 36540787
naming conventions?  Which ones?
0
 
LVL 75
ID: 36540809
The ones that have a prefix of 's' (which would imply String) that are actually numeric ...

mx
0
 
LVL 2

Author Comment

by:pskeens
ID: 36540878
Oh yeah, that may be a good thing.  I will change after this application is done. lol.  I don't want to go back and change all the code in this thing.  Maybe after its complete then I will go back and clean up.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now