473,589 Members | 2,558 Online
Bytes | Software Development & Data Engineering Community
+ 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_impo rt') 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.OpenR ecordset("tbl_g roup_import")

contact.MoveFir st

Do Until contact.EOF

title = contact.Fields. Item("contact_t itle")
first_name = contact.Fields. Item("contact_f irst_name")

insert_sql = "INSERT INTO
qry_group_membe r(group_member_ group_id,contac t_title,contact _first_name)"
& _
"VALUES (group_id,title ,first_name)"

DoCmd.RunSQL insert_sql

contact.MoveNex t

Loop
Sep 23 '08 #1
2 30138

"franc sutherland" <fr************ **@googlemail.c omwrote in message
news:26******** *************** ***********@k30 g2000hse.google groups.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_impo rt') 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.OpenR ecordset("tbl_g roup_import")

contact.MoveFir st

Do Until contact.EOF

title = contact.Fields. Item("contact_t itle")
first_name = contact.Fields. Item("contact_f irst_name")

insert_sql = "INSERT INTO
qry_group_membe r(group_member_ group_id,contac t_title,contact _first_name)"
& _
"VALUES (group_id,title ,first_name)"

DoCmd.RunSQL insert_sql

contact.MoveNex t

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.comwro te:
"franc sutherland" <franc.sutherl. ..@googlemail.c omwrote in message

news:26******** *************** ***********@k30 g2000hse.google groups.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_impo rt') 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.OpenR ecordset("tbl_g roup_import")
* * contact.MoveFir st
* * * * Do Until contact.EOF
* * * * * * title = contact.Fields. Item("contact_t itle")
* * * * * * first_name = contact.Fields. Item("contact_f irst_name")
* * * * * * insert_sql = "INSERT INTO
qry_group_membe r(group_member_ group_id,contac t_title,contact _first_name)"
& _
* * * * * * "VALUES (group_id,title ,first_name)"
* * * * * * DoCmd.RunSQL insert_sql
* * * * * * contact.MoveNex t
* * * * 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
4531
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! ----------------------------------------------------------------- I created a sign-up sheet (reg.html) where people fill in their first name, last name, email, etc. The data are then sent to a PHP script (reg.php). The data are then inserted into a table (reg) in MS SQL server. I have declared the...
0
2728
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, fname, lname) values
6
3237
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 potential problem). I changed all my variables in the insert statement to text to test as shown below. Still getting the error! I've checked again and again my db table and field names. Could someone please tell me where to go from here??? THANKS,...
11
16211
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, Col3, Col4) Values(" & _ str1 & "," & dt1 & "," & str2 & "," & str3 & ")" where str1 = "col1"
7
28740
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 (Tournament, MemberName, Score) VALUES (vtournament, vMemberName, vScore) SELECT ID, Tournament, MemberName, Score FROM Tournament WHERE (ID = @@IDENTITY);"
3
1821
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 table in question holds the various scan results of every scan from different computers. I have an insert statement that works however I am having troulbe getting and update statement out of it, not sure if I'm using the correct method to insert...
2
2834
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 radiolistbuton. I can't get the variable to pass through and insert into the db. It's an oracle 10g express db and the insert statement works when a normal string is netered but not with a variable and ideas? OracleTransaction txn =...
12
16109
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 form. Then depending on the difference between the start and end dates, that number of text boxes have their visibility set to TRUE and are displayed on the form. The user then enters volumes for each of the months between the start date and end...
0
7931
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
7865
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
8233
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8360
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7990
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5399
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3849
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
2374
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
0
1198
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.