Solved

strSQL with multiple criteria problem

Posted on 2011-09-14
12
832 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 - Microsoft MVP, Access and Data Platform) 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

740 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