473,323 Members | 1,560 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,323 software developers and data experts.

Unable to get values from a list box

Hi,

I am trying to use selected values in a list box on a form to create records in a table using a runSQL method. The list box is not bound to a field since i want to make multiple records from the selection. However, for some reason the VBA code below won't return the selected record. Only blank values which appear in the MsgBox. The function results in the invalid SQL syntax error "Syntax error in INSERT INTO statement". The statement works when I substitute text for the variables. So I am fairly confident the SQL statement is correct. Can someone look this over to see if I am missing something? Thanks.

Expand|Select|Wrap|Line Numbers
  1. Private Sub btnAddSample_Click()
  2. On Error GoTo Err_btnAddSample_Click
  3.  
  4.  
  5.     DoCmd.GoToRecord , , acNewRec
  6.     Dim varItem As Variant
  7.     Dim sampleName, analysisName, SQLstr As String
  8.     Dim analyList As Control
  9.     Set analyList = Forms![Enter Sample Information]![Analyses]
  10.     sampleName = Me.Sample_Name.Value
  11.     For Each varItem In analyList.ItemsSelected
  12.         analyisName = analyList.ItemData(varItem)
  13.         MsgBox (analysisName)
  14.         SQLstr = "INSERT INTO tblSampleAnalyses (Sample_Name,Analysis_Name) VALUES (" & sampleName & "," & analysisName & ");"
  15.         DoCmd.RunSQL SQLstr
  16.     Next varItem
  17.  
  18. Exit_btnAddSample_Click:
  19.     Exit Sub
  20.  
  21. Err_btnAddSample_Click:
  22.     MsgBox Err.Description
  23.     Resume Exit_btnAddSample_Click
  24.  
  25. End Sub
  26.  
Apr 2 '08 #1
3 1662
ADezii
8,834 Expert 8TB
Hi,

I am trying to use selected values in a list box on a form to create records in a table using a runSQL method. The list box is not bound to a field since i want to make multiple records from the selection. However, for some reason the VBA code below won't return the selected record. Only blank values which appear in the MsgBox. The function results in the invalid SQL syntax error "Syntax error in INSERT INTO statement". The statement works when I substitute text for the variables. So I am fairly confident the SQL statement is correct. Can someone look this over to see if I am missing something? Thanks.

Expand|Select|Wrap|Line Numbers
  1. Private Sub btnAddSample_Click()
  2. On Error GoTo Err_btnAddSample_Click
  3.  
  4.  
  5.     DoCmd.GoToRecord , , acNewRec
  6.     Dim varItem As Variant
  7.     Dim sampleName, analysisName, SQLstr As String
  8.     Dim analyList As Control
  9.     Set analyList = Forms![Enter Sample Information]![Analyses]
  10.     sampleName = Me.Sample_Name.Value
  11.     For Each varItem In analyList.ItemsSelected
  12.         analyisName = analyList.ItemData(varItem)
  13.         MsgBox (analysisName)
  14.         SQLstr = "INSERT INTO tblSampleAnalyses (Sample_Name,Analysis_Name) VALUES (" & sampleName & "," & analysisName & ");"
  15.         DoCmd.RunSQL SQLstr
  16.     Next varItem
  17.  
  18. Exit_btnAddSample_Click:
  19.     Exit Sub
  20.  
  21. Err_btnAddSample_Click:
  22.     MsgBox Err.Description
  23.     Resume Exit_btnAddSample_Click
  24.  
  25. End Sub
  26.  
This code should work quite nicely for you:
Expand|Select|Wrap|Line Numbers
  1. Dim varItem As Variant
  2. Dim sampleName As Variant
  3. Dim analysisName As Variant
  4. Dim SQLstr As String
  5. Dim analyList As ListBox
  6.  
  7. DoCmd.GoToRecord , , acNewRec
  8.  
  9. Set analyList = Forms![Enter Sample Information]![Analyses]
  10. sampleName = Me.Sample_Name.Value
  11.  
  12. If analyList.ItemsSelected.Count > 0 Then
  13.   For Each varItem In analyList.ItemsSelected
  14.     analysisName = analyList.ItemData(varItem)
  15.       DoCmd.SetWarnings False
  16.       SQLstr = "INSERT INTO tblSampleAnalyses ([Sample_Name], [Analysis_Name]) VALUES ('" & _
  17.                 sampleName & "', '" & analysisName & "')"
  18.         DoCmd.RunSQL SQLstr
  19.       DoCmd.SetWarnings True
  20.   Next varItem
  21. End If
Apr 2 '08 #2
This code should work quite nicely for you:
Expand|Select|Wrap|Line Numbers
  1. Dim varItem As Variant
  2. Dim sampleName As Variant
  3. Dim analysisName As Variant
  4. Dim SQLstr As String
  5. Dim analyList As ListBox
  6.  
  7. DoCmd.GoToRecord , , acNewRec
  8.  
  9. Set analyList = Forms![Enter Sample Information]![Analyses]
  10. sampleName = Me.Sample_Name.Value
  11.  
  12. If analyList.ItemsSelected.Count > 0 Then
  13.   For Each varItem In analyList.ItemsSelected
  14.     analysisName = analyList.ItemData(varItem)
  15.       DoCmd.SetWarnings False
  16.       SQLstr = "INSERT INTO tblSampleAnalyses ([Sample_Name], [Analysis_Name]) VALUES ('" & _
  17.                 sampleName & "', '" & analysisName & "')"
  18.         DoCmd.RunSQL SQLstr
  19.       DoCmd.SetWarnings True
  20.   Next varItem
  21. End If
Worked like a charm! Thanks!
Apr 4 '08 #3
ADezii
8,834 Expert 8TB
Worked like a charm! Thanks!
You are quite welcome.
Apr 4 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: TM | last post by:
When I run an ASP.Net application I am getting the following error: "Error while trying to run project: Unable to start debugging on the web server. The project is not configured to be debugged."...
6
by: Brad | last post by:
I have a win2003 server workstation with multiple webs, each web has it's own ip address. In VS2005, if I select to open an existing web site, select Local IIS, the dialog correctly displays a...
6
by: Ian Davies | last post by:
Hi me again, sorry to be a pain. Ive been struggling with this one all day. Hope you can understand whats happening. First my script is below. Have a look and I'll explain at the bottom what it...
0
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
3
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
0
by: nimjerry | last post by:
i am using db2 udb V 9 on aix 5.3 and in db2diag.log alwas has this error occurr below is sample message 2008-03-03-09.45.34.366406+420 I306667A443 LEVEL: Warning PID : 835622 ...
1
by: divya | last post by:
Hello, I used the below code to move selected items between two listboxes Left and Right:- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestPopup.aspx.cs" Inherits="TestPopup" %> ...
8
by: pereges | last post by:
Hi, I'm trying to deallocate a kd tree which I created dynamically. There were no problems creating the structure and I can access it easily but there is a problem while trying to free it. Here's...
4
by: keirnus | last post by:
Hello, I've been coding in Access VBA to create an Excel Graph and it was good. Until I got this error: Please check the code below: Private Sub TestGraph3()
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.