Solved

strSQL with multiple criteria problem

Posted on 2011-09-14
12
818 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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
 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
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.

831 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