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. - Private Sub btnAddSample_Click()
-
On Error GoTo Err_btnAddSample_Click
-
-
-
DoCmd.GoToRecord , , acNewRec
-
Dim varItem As Variant
-
Dim sampleName, analysisName, SQLstr As String
-
Dim analyList As Control
-
Set analyList = Forms![Enter Sample Information]![Analyses]
-
sampleName = Me.Sample_Name.Value
-
For Each varItem In analyList.ItemsSelected
-
analyisName = analyList.ItemData(varItem)
-
MsgBox (analysisName)
-
SQLstr = "INSERT INTO tblSampleAnalyses (Sample_Name,Analysis_Name) VALUES (" & sampleName & "," & analysisName & ");"
-
DoCmd.RunSQL SQLstr
-
Next varItem
-
-
Exit_btnAddSample_Click:
-
Exit Sub
-
-
Err_btnAddSample_Click:
-
MsgBox Err.Description
-
Resume Exit_btnAddSample_Click
-
-
End Sub
-
3 1662
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. - Private Sub btnAddSample_Click()
-
On Error GoTo Err_btnAddSample_Click
-
-
-
DoCmd.GoToRecord , , acNewRec
-
Dim varItem As Variant
-
Dim sampleName, analysisName, SQLstr As String
-
Dim analyList As Control
-
Set analyList = Forms![Enter Sample Information]![Analyses]
-
sampleName = Me.Sample_Name.Value
-
For Each varItem In analyList.ItemsSelected
-
analyisName = analyList.ItemData(varItem)
-
MsgBox (analysisName)
-
SQLstr = "INSERT INTO tblSampleAnalyses (Sample_Name,Analysis_Name) VALUES (" & sampleName & "," & analysisName & ");"
-
DoCmd.RunSQL SQLstr
-
Next varItem
-
-
Exit_btnAddSample_Click:
-
Exit Sub
-
-
Err_btnAddSample_Click:
-
MsgBox Err.Description
-
Resume Exit_btnAddSample_Click
-
-
End Sub
-
This code should work quite nicely for you: -
Dim varItem As Variant
-
Dim sampleName As Variant
-
Dim analysisName As Variant
-
Dim SQLstr As String
-
Dim analyList As ListBox
-
-
DoCmd.GoToRecord , , acNewRec
-
-
Set analyList = Forms![Enter Sample Information]![Analyses]
-
sampleName = Me.Sample_Name.Value
-
-
If analyList.ItemsSelected.Count > 0 Then
-
For Each varItem In analyList.ItemsSelected
-
analysisName = analyList.ItemData(varItem)
-
DoCmd.SetWarnings False
-
SQLstr = "INSERT INTO tblSampleAnalyses ([Sample_Name], [Analysis_Name]) VALUES ('" & _
-
sampleName & "', '" & analysisName & "')"
-
DoCmd.RunSQL SQLstr
-
DoCmd.SetWarnings True
-
Next varItem
-
End If
This code should work quite nicely for you: -
Dim varItem As Variant
-
Dim sampleName As Variant
-
Dim analysisName As Variant
-
Dim SQLstr As String
-
Dim analyList As ListBox
-
-
DoCmd.GoToRecord , , acNewRec
-
-
Set analyList = Forms![Enter Sample Information]![Analyses]
-
sampleName = Me.Sample_Name.Value
-
-
If analyList.ItemsSelected.Count > 0 Then
-
For Each varItem In analyList.ItemsSelected
-
analysisName = analyList.ItemData(varItem)
-
DoCmd.SetWarnings False
-
SQLstr = "INSERT INTO tblSampleAnalyses ([Sample_Name], [Analysis_Name]) VALUES ('" & _
-
sampleName & "', '" & analysisName & "')"
-
DoCmd.RunSQL SQLstr
-
DoCmd.SetWarnings True
-
Next varItem
-
End If
Worked like a charm! Thanks!
Worked like a charm! Thanks!
You are quite welcome.
Sign in to post your reply or Sign up for a free account.
Similar topics
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."...
|
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...
|
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...
|
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...
|
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...
|
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 ...
|
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" %>
...
|
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...
|
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()
|
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...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
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...
|
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...
| |