473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Multi-select List box with textPK

I'm having problems with the following,
I originally had a PKnumber EmployeeID then had to change it to a text
field so it would accept both numbers and letters. Now it doesn't
work. Any suggestions?

Private Sub List2_AfterUpdate()

Dim varItem As Variant
Dim strTemp As String
Dim strSQL As String
Dim db As DAO.Database
Dim qry As QueryDef

strSQL = "SELECT tblEmployees.EmployeeID, tblEmployees.[LName] & "",
"" & [FName] & "" "" & [MName] as Employees FROM tblEmployees "

For Each varItem In Me.ActiveControl.ItemsSelected
strTemp = strTemp & "((tblEmployees.EmployeeID) = " &
Me.ActiveControl.ItemData(varItem) & ") Or "
Next

strSQL = strSQL & "WHERE (" & Left(strTemp, Len(strTemp) - 4) &
");"

' On Error Resume Next
Set db = CurrentDb
With db
.QueryDefs.Delete "qryEmployee"
Set qry = .CreateQueryDef("qryEmployee", strSQL)

End With

End Sub

Thanks.

Nov 13 '05 #1
5 1896
I get an error message of data type mismatch in criteria expression.
Thanks.

Nov 13 '05 #2
If EmployeeID is text, you need to enclose the values in quotes:

For Each varItem In Me.ActiveControl.ItemsSelected
strTemp = strTemp & "((tblEmployees.EmployeeID) = " & Chr$(34)
Me.ActiveControl.ItemData(varItem) & Chr$(34) & ") Or "
Next

(Chr$(34) is ")

However, I think it would probably be better to use:

Private Sub List2_AfterUpdate()

Dim varItem As Variant
Dim strTemp As String
Dim strSQL As String
Dim db As DAO.Database
Dim qry As QueryDef

strSQL = "SELECT tblEmployees.EmployeeID, tblEmployees.[LName] & "",
"" & [FName] & "" "" & [MName] as Employees FROM tblEmployees "

For Each varItem In Me.ActiveControl.ItemsSelected
strTemp = strTemp & Chr$(34) & Me.ActiveControl.ItemData(varItem) &
Chr$(34) & ", "
Next

strSQL = strSQL & "WHERE tblEmployees.EmployeeID IN (" & Left(strTemp,
Len(strTemp) - 2) &
");"

' On Error Resume Next
Set db = CurrentDb
With db
.QueryDefs.Delete "qryEmployee"
Set qry = .CreateQueryDef("qryEmployee", strSQL)

End With

End Sub

Otherwise, your SQL string may grow to be too large.
--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

<Ly***********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I'm having problems with the following,
I originally had a PKnumber EmployeeID then had to change it to a text
field so it would accept both numbers and letters. Now it doesn't
work. Any suggestions?

Private Sub List2_AfterUpdate()

Dim varItem As Variant
Dim strTemp As String
Dim strSQL As String
Dim db As DAO.Database
Dim qry As QueryDef

strSQL = "SELECT tblEmployees.EmployeeID, tblEmployees.[LName] & "",
"" & [FName] & "" "" & [MName] as Employees FROM tblEmployees "

For Each varItem In Me.ActiveControl.ItemsSelected
strTemp = strTemp & "((tblEmployees.EmployeeID) = " &
Me.ActiveControl.ItemData(varItem) & ") Or "
Next

strSQL = strSQL & "WHERE (" & Left(strTemp, Len(strTemp) - 4) &
");"

' On Error Resume Next
Set db = CurrentDb
With db
.QueryDefs.Delete "qryEmployee"
Set qry = .CreateQueryDef("qryEmployee", strSQL)

End With

End Sub

Thanks.

Nov 13 '05 #3
Thanks for your reply but I'm still getting Data type mismatch in
criteria expression error. I'm not an expert at writting this stuff
and with your assistance this is what I think you suggested?

Private Sub List2_AfterUpdate()

Dim varItem As Variant
Dim strTemp As String
Dim strSQL As String
Dim db As DAO.Database
Dim qry As QueryDef

strSQL = "SELECT tblEmployees.EmployeeID, tblEmployees.[LName] & "",
"" & [FName] & "" "" & [MName] as Employees FROM tblEmployees "

For Each varItem In Me.ActiveControl.ItemsSelected
strTemp = strTemp & "((tblEmployees.EmployeeID) = " & Chr$(34) &
Me.ActiveControl.ItemData(varItem) & Chr$(34) & ", "
Next
strSQL = strSQL & "WHERE tblEmployees.EmployeeID IN (" & Left(strTemp,
Len(strTemp) - 2) & ");"

' On Error Resume Next
Set db = CurrentDb
With db
.QueryDefs.Delete "qryEmployee"
Set qry = .CreateQueryDef("qryEmployee", strSQL)
End With
End Sub
Thanks for your help.

Nov 13 '05 #4
No, it's not what I suggested. Rather than just copying the code I gave you,
you modified your existing code so that you've got a blend of the two
methods.

You originally were using

WHERE tblEmployees.EmployeeID = "A" OR tblEmployees.EmployeeID = "B"

and I suggested changing it to

WHERE tblEmployees.EmployeeID IN ("A", "B").

What you're getting instead is

WHERE tblEmployees.EmployeeID IN (tblEmployees.EmployeeID = "A",
tblEmployees.EmployeeID = "B")

The line of code

strTemp = strTemp & "((tblEmployees.EmployeeID) = " & Chr$(34) &
Me.ActiveControl.ItemData(varItem) & Chr$(34) & ", "

should be

strTemp = strTemp & Chr$(34) & Me.ActiveControl.ItemData(varItem) & Chr$(34)
& ", "
--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

<Ly***********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Thanks for your reply but I'm still getting Data type mismatch in
criteria expression error. I'm not an expert at writting this stuff
and with your assistance this is what I think you suggested?

Private Sub List2_AfterUpdate()

Dim varItem As Variant
Dim strTemp As String
Dim strSQL As String
Dim db As DAO.Database
Dim qry As QueryDef

strSQL = "SELECT tblEmployees.EmployeeID, tblEmployees.[LName] & "",
"" & [FName] & "" "" & [MName] as Employees FROM tblEmployees "

For Each varItem In Me.ActiveControl.ItemsSelected
strTemp = strTemp & "((tblEmployees.EmployeeID) = " & Chr$(34) &
Me.ActiveControl.ItemData(varItem) & Chr$(34) & ", "
Next
strSQL = strSQL & "WHERE tblEmployees.EmployeeID IN (" & Left(strTemp,
Len(strTemp) - 2) & ");"

' On Error Resume Next
Set db = CurrentDb
With db
.QueryDefs.Delete "qryEmployee"
Set qry = .CreateQueryDef("qryEmployee", strSQL)
End With
End Sub
Thanks for your help.

Nov 13 '05 #5
Works well, thanks for your help.

Lynn

Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: ajikoe | last post by:
Hello, Is anyone has experiance in running python code to run multi thread parallel in multi processor. Is it possible ? Can python manage which cpu shoud do every thread? Sincerely Yours,...
4
by: Frank Jona | last post by:
Intellisense with C# and a multi-file assembly is not working. With VB.NET it is working. Is there a fix availible? We're using VisualStudio 2003 Regards Frank
12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
0
by: frankenberry | last post by:
I have multi-page tiff files. I need to extract individual frames from the multi-page tiffs and save them as single-page tiffs. 95% of the time I receive multi-page tiffs containing 1 or more black...
6
by: cody | last post by:
What are multi file assemblies good for? What are the advantages of using multiple assemblies (A.DLL+B.DLL) vs. a single multi file assembly (A.DLL+A.NETMODULE)?
4
by: mimmo | last post by:
Hi! I should convert the accented letters of a string in the correspondent letters not accented. But when I compile with -Wall it give me: warning: multi-character character constant Do the...
5
by: bobwansink | last post by:
Hi, I'm relatively new to programming and I would like to create a C++ multi user program. It's for a project for school. This means I will have to write a paper about the theory too. Does anyone...
17
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, Wide character and multi-byte character are two popular encoding schemes on Windows. And wide character is using unicode encoding scheme. But each time I feel confused when...
0
by: Sabri.Pllana | last post by:
We apologize if you receive multiple copies of this call for papers. *********************************************************************** 2008 International Workshop on Multi-Core Computing...
2
by: Aussie Rules | last post by:
Hi, I have a site that Iwant to either display my text in english or french, based on the users prefernces ? I am new to webforms, but I know in winforms, this is pretty easy with a resource...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.