473,480 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using variables in INSERT INTO statement

Hello,

I am using Access 2003.

Is it possible to use string variables in the INSERT INTO statement?

I am using the INSERT INTO statement to add a long list of contacts to
a group by looping through the recordset (based on a linked
spreadsheet, 'tbl_group_import') and using the INSERT INTO statement
on each loop. I am using the rst.Fields.Item("FieldName") method to
assign a value to the string variables. However, when I run the code,
it prompts me for the variable values.

I am executing the SQL using DoCmd.RunSQL.

I would really appreciate any assistance.

Thanks,

Franc.

Here is the code;

Dim contact As Recordset

Dim title As String
Dim first_name As String
Dim insert_sql As String
Dim group_id As Integer

group_id = Forms![frm_group_view]![group_id]

Set contact = CurrentDb.OpenRecordset("tbl_group_import")

contact.MoveFirst

Do Until contact.EOF

title = contact.Fields.Item("contact_title")
first_name = contact.Fields.Item("contact_first_name")

insert_sql = "INSERT INTO
qry_group_member(group_member_group_id,contact_tit le,contact_first_name)"
& _
"VALUES (group_id,title,first_name)"

DoCmd.RunSQL insert_sql

contact.MoveNext

Loop
Sep 23 '08 #1
2 30094

"franc sutherland" <fr**************@googlemail.comwrote in message
news:26**********************************@k30g2000 hse.googlegroups.com...
Hello,

I am using Access 2003.

Is it possible to use string variables in the INSERT INTO statement?

I am using the INSERT INTO statement to add a long list of contacts to
a group by looping through the recordset (based on a linked
spreadsheet, 'tbl_group_import') and using the INSERT INTO statement
on each loop. I am using the rst.Fields.Item("FieldName") method to
assign a value to the string variables. However, when I run the code,
it prompts me for the variable values.

I am executing the SQL using DoCmd.RunSQL.

I would really appreciate any assistance.

Thanks,

Franc.

Here is the code;

Dim contact As Recordset

Dim title As String
Dim first_name As String
Dim insert_sql As String
Dim group_id As Integer

group_id = Forms![frm_group_view]![group_id]

Set contact = CurrentDb.OpenRecordset("tbl_group_import")

contact.MoveFirst

Do Until contact.EOF

title = contact.Fields.Item("contact_title")
first_name = contact.Fields.Item("contact_first_name")

insert_sql = "INSERT INTO
qry_group_member(group_member_group_id,contact_tit le,contact_first_name)"
& _
"VALUES (group_id,title,first_name)"

DoCmd.RunSQL insert_sql

contact.MoveNext

Loop
With your current code you will need to pass the values to the query.
Assuming there are no special characters in title or first_name.

"VALUES(" & group_id & ",""" & title & """,""" & first_name & """)"

Other choices
1) Save your query, converting the variables to parameters. Then pass the
values though the parameters.
2) Link to or import the spreadsheet and modify the query to append all the
records at once.
3) Open a recordset to your table to insert the records.
Sep 23 '08 #2
On Sep 23, 1:18*pm, "paii, Ron" <n...@no.comwrote:
"franc sutherland" <franc.sutherl...@googlemail.comwrote in message

news:26**********************************@k30g2000 hse.googlegroups.com...
Hello,
I am using Access 2003.
Is it possible to use string variables in the INSERT INTO statement?
I am using the INSERT INTO statement to add a long list of contacts to
a group by looping through the recordset (based on a linked
spreadsheet, 'tbl_group_import') and using the INSERT INTO statement
on each loop. *I am using the rst.Fields.Item("FieldName") method to
assign a value to the string variables. *However, when I run the code,
it prompts me for the variable values.
I am executing the SQL using DoCmd.RunSQL.
I would really appreciate any assistance.
Thanks,
Franc.
Here is the code;
Dim contact As Recordset
* * Dim title As String
* * Dim first_name As String
* * Dim insert_sql As String
* * Dim group_id As Integer
* * group_id = Forms![frm_group_view]![group_id]
* * Set contact = CurrentDb.OpenRecordset("tbl_group_import")
* * contact.MoveFirst
* * * * Do Until contact.EOF
* * * * * * title = contact.Fields.Item("contact_title")
* * * * * * first_name = contact.Fields.Item("contact_first_name")
* * * * * * insert_sql = "INSERT INTO
qry_group_member(group_member_group_id,contact_tit le,contact_first_name)"
& _
* * * * * * "VALUES (group_id,title,first_name)"
* * * * * * DoCmd.RunSQL insert_sql
* * * * * * contact.MoveNext
* * * * Loop

With your current code you will need to pass the values to the query.
Assuming there are no special characters in title or first_name.

"VALUES(" & group_id & ",""" & title & """,""" & first_name & """)"

Other choices
1) Save your query, converting the variables to parameters. Then pass the
values though the parameters.
2) Link to or import the spreadsheet and modify the query to append all the
records at once.
3) Open a recordset to your table to insert the records.
Thanks Ron,
That's sorted it.
All the best,
Franc.
Sep 23 '08 #3

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

Similar topics

1
4527
by: newbie_mw | last post by:
Seems my post was buried in more cries for help :-) I will try again. It's probably a very novice question so please take a look! Thanks!...
0
2721
by: Ted Greek | last post by:
How do I get two values for two different fields in an insert statement values clause using one select? For instance: declare @test int @test gets assigned... insert into (id, field1,...
6
3230
by: Kathy Burke | last post by:
Ugh. I'm using the following in an asp.net. I get an Syntax Error in INSERT INTO Statement on line Cmd1.ExecuteNonQuery(). I've made all my database fields text (just to eliminate that as a...
11
16185
by: sm | last post by:
Hi All, Can anybody give me the syntax to insert a record into SQL server through VB code using variables? The following statement is failing! sInsertQuery = "INSERT INTO TestTab (Col1, Col2,...
7
28727
by: Cindy H | last post by:
Hi I'm having a problem getting the insert statement correct for an Access table I'm using. The Access table uses an autonumber for the primary key. I have tried this: INSERT INTO Tournaments...
3
1814
by: rhaazy | last post by:
Using ms sql 2000 I have 2 tables. I have a table which has information regarding a computer scan. Each record in this table has a column called MAC which is the unique ID for each Scan. The...
2
2825
by: btuisee | last post by:
I'm having an issue getting a variable to pass through to an oracle database. I have an insert statment and one of the values are a variable called q1. Q1 is a variable that gets it's value from a...
12
16090
by: Blake Rice | last post by:
Hi everyone! This is my first time posting here, so here goes. THE SITUATION: I have a form that has a start date, end date, project number, and total volume. These values are pulled from another...
0
7048
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
7050
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7091
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...
1
6743
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6966
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...
0
5344
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,...
1
4787
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...
0
2988
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
185
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...

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.