473,659 Members | 2,562 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with listbox and append VBA

Hi

I'm attempting to append multiple values into a new record, using
multiple criteria from a listbox. I've got the basics for the code
below but I get an "Error 3085 Undefined Function" for the Dlookup part
of it.
Dim db As Database
Dim rec As DAO.Recordset
Dim varSelected As Variant
' Now loop through your selected items and place each selected item
into the table
' as an individual record
Set db = CurrentDb()
Set rec = db.OpenRecordse t("tblReportCom ments")
For Each varSelected In Me!lstGrpMem.It emsSelected
rec.AddNew
rec("[Group Member ID]") = Me!lstGrpMem.Co lumn(0, varSelected)
'Access Debug highlights this code below:
rec("[Basic Skills and Tactics]") = DLookup("[Basic Skills and
Tactics]", "tblAttainmentI ndicators", _
"[Attainment Level]= Me!lstGrpMem.Co lumn(6, varSelected)And [Attainment
Gender] = Me!lstGrpMem.Co lumn(4,varSelec ted)")
rec.Update
Next varSelected
Set db = Nothing ' Clear db...
MsgBox ("This data is now in your table..")

Grateful for any help and advice.

David

Feb 1 '06 #1
6 5786
Gox
de********@gmai l.com wrote:
Hi

I'm attempting to append multiple values into a new record, using
multiple criteria from a listbox. I've got the basics for the code
below but I get an "Error 3085 Undefined Function" for the Dlookup part
of it.
Dim db As Database
Dim rec As DAO.Recordset
Dim varSelected As Variant
' Now loop through your selected items and place each selected item
into the table
' as an individual record
Set db = CurrentDb()
Set rec = db.OpenRecordse t("tblReportCom ments")
For Each varSelected In Me!lstGrpMem.It emsSelected
rec.AddNew
rec("[Group Member ID]") = Me!lstGrpMem.Co lumn(0, varSelected)
'Access Debug highlights this code below:
rec("[Basic Skills and Tactics]") = DLookup("[Basic Skills and
Tactics]", "tblAttainmentI ndicators", _
"[Attainment Level]= Me!lstGrpMem.Co lumn(6, varSelected)And [Attainment
Gender] = Me!lstGrpMem.Co lumn(4,varSelec ted)")
rec.Update
Next varSelected
Set db = Nothing ' Clear db...
MsgBox ("This data is now in your table..")

Grateful for any help and advice.

David

David, try this:

DLookup("[Basic Skills and
Tactics]", "tblAttainmentI ndicators", _
"[Attainment Level]=" & Me!lstGrpMem.Co lumn(6, varSelected) & " And
[Attainment Gender]=" & Me!lstGrpMem.Co lumn(4,varSelec ted))
Feb 1 '06 #2
<de********@gma il.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
Hi

I'm attempting to append multiple values into a new record, using
multiple criteria from a listbox. I've got the basics for the code
below but I get an "Error 3085 Undefined Function" for the Dlookup part
of it.
Dim db As Database
Dim rec As DAO.Recordset
Dim varSelected As Variant
' Now loop through your selected items and place each selected item
into the table
' as an individual record
Set db = CurrentDb()
Set rec = db.OpenRecordse t("tblReportCom ments")
For Each varSelected In Me!lstGrpMem.It emsSelected
rec.AddNew
rec("[Group Member ID]") = Me!lstGrpMem.Co lumn(0, varSelected)
'Access Debug highlights this code below:
rec("[Basic Skills and Tactics]") = DLookup("[Basic Skills and
Tactics]", "tblAttainmentI ndicators", _
"[Attainment Level]= Me!lstGrpMem.Co lumn(6, varSelected)And [Attainment
Gender] = Me!lstGrpMem.Co lumn(4,varSelec ted)")
rec.Update
Next varSelected
Set db = Nothing ' Clear db...
MsgBox ("This data is now in your table..")

Grateful for any help and advice.

David


You are missing quotes in the last argument of the dlookup bit. However, we
can't be sure how many quotes you are missing because we don't know if any
of the columns are text columns. The result needs to be something like:
MyNumeric=123 AND MyText="Whateve r"
I would dimension a separate variable, strWhere, to hold this last bit and
build it up slowly and carefully, before using it in the dlookup function.
In fact, you could do a debug.print or msgbox for the strWhere to make sure
it is properly formed.
A final thought is that the dlookup is slow and calling it repeatedly is
often to be avoided. You may have so few records to append that it makes
little difference, but you could open a read-only recordset which has the
necessary data from tblAttainmentIn dicators and loop through this to add the
records.
Feb 1 '06 #3
Hi

Thanks for both replies. I've tried both suggestions and for both I
now get Run Time Error 2471: The expression you entered as a query
parameter produced this error: "The Object doesn't contain the
automation object 'Male."

"Male" is the gender of the Dlookup end criteria, and so is a text
field. I'm now assuming I'm missing quotes somewhere?

Here is my amended code:

--------------------
Dim db As Database
Dim rec As DAO.Recordset
Dim varSelected As Variant
Dim strWhere As String
' Now loop through your selected items and place each selected item
into the table
' as an individual record
Set db = CurrentDb()
Set rec = db.OpenRecordse t("tblReportCom ments")
For Each varSelected In Me!lstGrpMem.It emsSelected
strWhere = Me!lstGrpMem.Co lumn(4, varSelected)
rec.AddNew
rec("[Group Member ID]") = Me!lstGrpMem.Co lumn(0, varSelected)
rec("[Basic Skills and Tactics]") = DLookup("[Basic Skills and
Tactics]", "tblAttainmentI ndicators", _
"[Attainment Level]=" & Me!lstGrpMem.Co lumn(6, varSelected) & " And
[Attainment Gender] = " & strWhere)
rec.Update
Next varSelected
Set db = Nothing ' Clear db...
MsgBox ("This data is now in your table..")
--------------

Again thanks for any help.

David

Feb 1 '06 #4

<de********@gma il.com> wrote in message
news:11******** *************@g 44g2000cwa.goog legroups.com...
Hi

Thanks for both replies. I've tried both suggestions and for both I
now get Run Time Error 2471: The expression you entered as a query
parameter produced this error: "The Object doesn't contain the
automation object 'Male."

"Male" is the gender of the Dlookup end criteria, and so is a text
field. I'm now assuming I'm missing quotes somewhere?

Here is my amended code:

--------------------
Dim db As Database
Dim rec As DAO.Recordset
Dim varSelected As Variant
Dim strWhere As String
' Now loop through your selected items and place each selected item
into the table
' as an individual record
Set db = CurrentDb()
Set rec = db.OpenRecordse t("tblReportCom ments")
For Each varSelected In Me!lstGrpMem.It emsSelected
strWhere = Me!lstGrpMem.Co lumn(4, varSelected)
rec.AddNew
rec("[Group Member ID]") = Me!lstGrpMem.Co lumn(0, varSelected)
rec("[Basic Skills and Tactics]") = DLookup("[Basic Skills and
Tactics]", "tblAttainmentI ndicators", _
"[Attainment Level]=" & Me!lstGrpMem.Co lumn(6, varSelected) & " And
[Attainment Gender] = " & strWhere)
rec.Update
Next varSelected
Set db = Nothing ' Clear db...
MsgBox ("This data is now in your table..")
--------------

Again thanks for any help.

David


Do not be afraid of using too many variables, lots of white space and
comments - I can't say I've tested this, but I think this is right. You can
see how I build up the strWhere surrounding the text column with quotes and
then try to get a value from tblAttainmentIn dicators. If I do, I add the
record.
Dim lngMemberID As Long
Dim lngLevel As Long
Dim strGender As String
Dim strWhere As String
Dim varAttainment As Variant

With Me.lstGrpMem
For Each varSelected In .ItemsSelected
lngMemberID = CLng(.Column(0, varSelected))
lngLevel = CLng(.Column(6, varSelected)
strGender = .Column(4, varSelected)
strWhere = """" & strGender & """"
strWhere = "[Attainment Gender]=" & strWhere
strWhere = strWhere & " AND [Attainment Level]="
strWhere = strWhere & CStr(lngLevel)
varAttainment = DLookup("[Basic Skills and Tactics]",
"tblAttainmentI ndicators", strWhere)
If Not IsNull(varAttai nment) Then
rec.AddNew
rec.Fields("[Group Member ID]") = lngMemberID
rec.Fields("[Basic Skills and Tactics]") = CLng(varAttainm ent)
rec.Update
End If
Next varSelected
End With
Hope this works without error, but you may need to tweak it - the point is
to do it slowly.

As I mentioned before, repeated use of dlookup can slow things down or cause
other problems. You could perhaps set up the listbox based on a query, so
that one of the columns holds the attainment - so you can get this value
directly from one of the listbox columns without looking it up later.

A further point is that, when you are adding multiple records like this, you
should wrap it in a transaction. This ensures that you either add all the
required records or you add none. As the code is currently, there may be
some error which means that you get half way through the loop and some
records are added and some are not.



Feb 1 '06 #5
Anthony thanks again for you help and patience!

Right- I've had a go with your code and I get an error 3464 Data
type-mistmatch in criteria expression:

varAttainment = DLookup("[Basic Skills and Tactics]",
"tblAttainmentI ndicators", strWhere)

I then tweaked the earlier code with some of your suggestions and it
adds a new record with the correct Group Member ID and with no apparent
errors- but does not add anything for the Basic Skills and Tactics. I
tried separating the variables in a MsgBox to see if they were set at
the right value and they are- attain brings the correct attianment
level from the listbox column 6 and strWhere brings the correct gender
from listbox column 4. I then tried assigning a variable to the entire
LookUp bit and msgBox this and it results in a null value error- so I
presume it's not DlookingUp properly!?

Here is my code:

----------------------------
Dim db As Database
Dim rec As DAO.Recordset
Dim varSelected As Variant
Dim strWhere As String
Dim attain As Long

' Now loop through your selected items and place each selected item
into the table
' as an individual record
Set db = CurrentDb()
Set rec = db.OpenRecordse t("tblReportCom ments")

For Each varSelected In Me!lstGrpMem.It emsSelected
strWhere = lstGrpMem.Colum n(4, varSelected)
attain = lstGrpMem.Colum n(6, varSelected)
rec.AddNew
rec.Fields("[Group Member ID]") = Me!lstGrpMem.Co lumn(0,
varSelected)
rec.Fields("[Basic Skills and Tactics]") = DLookup("[Basic Skills
and Tactics]", "tblAttainmentI ndicators", _
"[Attainment Level]=" & attain & " And [Attainment Gender] = " & """
strWhere""")
rec.Update
Next varSelected

----------------------------

I do want to be able to loop this for potentially hundreds of records
selected in my list box but I don't know what you mean by wrap it in a
transaction. I guess what I want to do is far exceeding my Access/VBA
skills, which is very frustrating!

thanks

David

Feb 2 '06 #6
<de********@gma il.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .
Anthony thanks again for you help and patience!

Right- I've had a go with your code and I get an error 3464 Data
type-mistmatch in criteria expression:

varAttainment = DLookup("[Basic Skills and Tactics]",
"tblAttainmentI ndicators", strWhere)

I then tweaked the earlier code with some of your suggestions and it
adds a new record with the correct Group Member ID and with no apparent
errors- but does not add anything for the Basic Skills and Tactics. I
tried separating the variables in a MsgBox to see if they were set at
the right value and they are- attain brings the correct attianment
level from the listbox column 6 and strWhere brings the correct gender
from listbox column 4. I then tried assigning a variable to the entire
LookUp bit and msgBox this and it results in a null value error- so I
presume it's not DlookingUp properly!?

Here is my code:

----------------------------
Dim db As Database
Dim rec As DAO.Recordset
Dim varSelected As Variant
Dim strWhere As String
Dim attain As Long

' Now loop through your selected items and place each selected item
into the table
' as an individual record
Set db = CurrentDb()
Set rec = db.OpenRecordse t("tblReportCom ments")

For Each varSelected In Me!lstGrpMem.It emsSelected
strWhere = lstGrpMem.Colum n(4, varSelected)
attain = lstGrpMem.Colum n(6, varSelected)
rec.AddNew
rec.Fields("[Group Member ID]") = Me!lstGrpMem.Co lumn(0,
varSelected)
rec.Fields("[Basic Skills and Tactics]") = DLookup("[Basic Skills
and Tactics]", "tblAttainmentI ndicators", _
"[Attainment Level]=" & attain & " And [Attainment Gender] = " & """
strWhere""")
rec.Update
Next varSelected

----------------------------

I do want to be able to loop this for potentially hundreds of records
selected in my list box but I don't know what you mean by wrap it in a
transaction. I guess what I want to do is far exceeding my Access/VBA
skills, which is very frustrating!

thanks

David


Hi David
Perhaps we need to do one basic thing first:
Ensure we can get a single line of code working involving DLookup.
Find a valid entry in your table, such as Gender=Male and Level=4 and note
the value of the field it should return. Now press CTRL-G to bring up the
immediate window and type in the following:

?DLookup("[Basic Skills and Tactics]", "tblAttainmentI ndicators",
"[Attainment Gender]=""Male"" AND [Attainment Level]=4")

This should return the value you noted down if it doesn't, then there is a
basic problem such as the field or table name being mis-spelled. I'll send
you an e-mail so you can contact me directly if you need to.
Feb 2 '06 #7

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

Similar topics

5
10604
by: Andrew | last post by:
Hi I just started learning wxPython I wanted to know how I could do this in wxPython self.listbox.delete(0, END) for item in self.results: self.listbox.insert(END, item)
1
2221
by: José Carlos | last post by:
Hi. How can i do to using a listbox.append(), it adds item in the first line.? Regards Jose.
1
2110
by: Steven M | last post by:
Hello!!! I dont know what is the problem!!! I am desperate!!! I want to put enabled = false a button when in ListBox has selected more than a item. this is the javascript. it dosen't Work!!!! <script language =javascript>
9
1516
by: Deano | last post by:
I need to knock something up quickly to allow a teacher to record incidents at a school. This means logging the type of incident e.g bullying, fight etc and the pupils that were involved. So I have tblPupils, tblPupilEventLink (the junction table) and tblIncidents. tblPupils relates to the junction table on PupilID and tblIncidents relates using IncidentID, as this is clearly a many to many relationship - many events can involve a...
6
9953
by: Mark | last post by:
Hello. I have a listbox whose rowsource is set to a saved query (call it "qry_customer_list.") When I add customers to my database, I call the listbox Requery method so that the listbox will display the customers that I added. Unfortunately, the listbox display does not change. If I close the form and reload it, then the listbox will show the new customers. Any suggestions on how to fix this?
1
3191
by: karups | last post by:
Hi, I've got listbox in my .aspx page where the users can make multiple selection. So, Users can select 7 items in listbox, I have to take value from items and pass it to stored procedure to extract a dataset back. 1.What should i do while passing the multiple selected values 2.Can i use 'in' clause in SQL procedure like eg:Select a.xxx from a where a.yyy in @y
3
2544
by: rn5a | last post by:
I am not very sure whether I should have continued with my earlier thread or started a new thread whose subject matter was somewhat similar to the subject matter in this thread. Anyway after much deliberation, I decided to start a new thread. So here it is. Pardon me if I should have continued with my earlier thread instead of starting this new thread. A ListBox lists all the directories & files existing in a directory on the server....
2
3093
by: Jeff | last post by:
Could someone tell me if it is possible to for a listbox displayed on an ..aspx web page to display more than a single space between words. all of the various test code below simply results in the display of a single space between "Hello" and "test" Thanks Jeff
6
15445
by: kimiraikkonen | last post by:
Hi, I can read from a text file and insert text file's contents into a listbox whose format is line by line using this code: Dim reader As String reader = My.Computer.FileSystem.ReadAllText("c: \bookmarks.txt") Dim strs() As String strs = Split(reader, Environment.NewLine)
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8339
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8629
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7360
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4176
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2757
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.