473,396 Members | 2,121 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,396 software developers and data experts.

SQL Insert from VBA to Access Database

Hi. My first post on the forum and I hope someone can help with this problem. I have a table called 'Inventory'. It has 11 fields, two of them being 'Num' and 'Plant'. There are a number of 'Plants' and the corresponding number ranges within those plants are:

Plant Number
TBM 1000 - 1099
ABC 1100 - 1199
AMS 1200 - 1299
BBR 1300 - 1399
etc.

So for TBM for example, it may have four existing records, 1000, 1001, 1002, 1003

To insert the next record for TBM, I would have to know the next Number
value, which would be 1004. If I set all the values for the fields in the
table Inventory from a VBA form manulally, I can use the following code to Insert the record. (var1 corresponds to the 'Number' value from the form and var2 the 'Plant' value from the form).

appAccess.DoCmd.RunSQL _
"(insert into Inventory values ('" _
& var1 & "', '" _
& var2 & "', '" _
& var3 & "', '" _
& var4 & "', '" _
& var5 & "', '" _
& var6 & "', '" _
& var7 & "', '" _
& var8 & "', '" _
& var9 & "', '" _
& var10 & "', '" _
& var11 & "'))"

However, what I want to do is create an Insert that will calculate the next
available Number based on the Plant value, in the case of TBM, 1004. I have
tried the following but it doesn't seem to work, although the nested select
works fine on its own. Any ideas.

appAccess.DoCmd.RunSQL _
"(insert into Inventory values ('( _
select max (Number) + 1 from Inventory where Plant = `" & var2 & "`)', '" _
& var2 & "', '" _
& var3 & "', '" _
& var4 & "', '" _
& var5 & "', '" _
& var6 & "', '" _
& var7 & "', '" _
& var8 & "', '" _
& var9 & "', '" _
& var10 & "', '" _
& var11 & "'))"

All code is VBA 6.3 from excel and the database is Access 2003 SP2. If you need anymore code, let me know.

Many thanks in advance.

Martin.
Jul 17 '07 #1
14 6416
abolos
65
Hi. My first post on the forum and I hope someone can help with this problem. I have a table called 'Inventory'. It has 11 fields, two of them being 'Num' and 'Plant'. There are a number of 'Plants' and the corresponding number ranges within those plants are:

Plant Number
TBM 1000 - 1099
ABC 1100 - 1199
AMS 1200 - 1299
BBR 1300 - 1399
etc.

So for TBM for example, it may have four existing records, 1000, 1001, 1002, 1003

To insert the next record for TBM, I would have to know the next Number
value, which would be 1004. If I set all the values for the fields in the
table Inventory from a VBA form manulally, I can use the following code to Insert the record. (var1 corresponds to the 'Number' value from the form and var2 the 'Plant' value from the form).

appAccess.DoCmd.RunSQL _
"(insert into Inventory values ('" _
& var1 & "', '" _
& var2 & "', '" _
& var3 & "', '" _
& var4 & "', '" _
& var5 & "', '" _
& var6 & "', '" _
& var7 & "', '" _
& var8 & "', '" _
& var9 & "', '" _
& var10 & "', '" _
& var11 & "'))"

However, what I want to do is create an Insert that will calculate the next
available Number based on the Plant value, in the case of TBM, 1004. I have
tried the following but it doesn't seem to work, although the nested select
works fine on its own. Any ideas.

appAccess.DoCmd.RunSQL _
"(insert into Inventory values ('( _
select max (Number) + 1 from Inventory where Plant = `" & var2 & "`)', '" _
& var2 & "', '" _
& var3 & "', '" _
& var4 & "', '" _
& var5 & "', '" _
& var6 & "', '" _
& var7 & "', '" _
& var8 & "', '" _
& var9 & "', '" _
& var10 & "', '" _
& var11 & "'))"

All code is VBA 6.3 from excel and the database is Access 2003 SP2. If you need anymore code, let me know.

Many thanks in advance.

Martin.

Why not try this:
Select MAX(NUM) where NUM= TBM
text0.value=MAX(NUM)
text0.value=NUM+1
Jul 17 '07 #2
JKing
1,206 Expert 1GB
Assuming your field Number is actually a number data type.
You could assign the incremented value to var1 before trying to insert.
Expand|Select|Wrap|Line Numbers
  1. var1 = DMax("[Number]", "Inventory", "[Plant] = '" & var2 & "'") + 1
  2.  
  3. appAccess.DoCmd.RunSQL _
  4. "(insert into Inventory values (" _
  5. & var1 & ", '" _
  6. & var2 & "', '" _
  7. & var3 & "', '" _
  8. & var4 & "', '" _
  9. & var5 & "', '" _
  10. & var6 & "', '" _
  11. & var7 & "', '" _
  12. & var8 & "', '" _
  13. & var9 & "', '" _
  14. & var10 & "', '" _
  15. & var11 & "'))"
  16.  
Also I removed the single quotes from var1 because if it's a number value you don't need to use them. Unless the field Number is a text data type in which case the Dmax function won't work properly
Jul 17 '07 #3
Assuming your field Number is actually a number data type.
You could assign the incremented value to var1 before trying to insert.
Expand|Select|Wrap|Line Numbers
  1. var1 = DMax("[Number]", "Inventory", "[Plant] = '" & var2 & "'") + 1
  2.  
  3. appAccess.DoCmd.RunSQL _
  4. "(insert into Inventory values (" _
  5. & var1 & ", '" _
  6. & var2 & "', '" _
  7. & var3 & "', '" _
  8. & var4 & "', '" _
  9. & var5 & "', '" _
  10. & var6 & "', '" _
  11. & var7 & "', '" _
  12. & var8 & "', '" _
  13. & var9 & "', '" _
  14. & var10 & "', '" _
  15. & var11 & "'))"
  16.  
Also I removed the single quotes from var1 because if it's a number value you don't need to use them. Unless the field Number is a text data type in which case the Dmax function won't work properly
Hi JKing

I like the idea of createing the var1 value before the original insert statement rather than the nested select. var1 was originally type string so I changed it to type Number. (Num is also type Number in the access database). When I try to run the insert, the VBA debugger stops at the

dim var1 as Number

with "Compile Error - User defined type not defined"

Any ideas?

Martin
Jul 17 '07 #4
JKing
1,206 Expert 1GB
Yeah just change it to the following:

Expand|Select|Wrap|Line Numbers
  1. Dim var1 As Integer
  2.  
Jul 17 '07 #5
Yeah just change it to the following:

Expand|Select|Wrap|Line Numbers
  1. Dim var1 As Integer
  2.  
Hi JKing

Sorry to be a pain here, but having changed var1 to Integer, the debugger stops at the following line (highlighting DMax)

var1 = DMax("[Num]", "Inventory", "[Plant] = '" & var2 & "'") + 1

with the following error.

"Compile Error - Sub or Function Not Defined.

Do I need to add in something from TOOLS/REFERENCES ?

Martin.
Jul 17 '07 #6
Hi

I got the code to work you gave me JKing. I just needed to include

Microsoft Access 11.0 Object Library

from Tool/References, and it works great.

Thanks for your help.

Martin.
Jul 18 '07 #7
JKing
1,206 Expert 1GB
You're welcome. Glad it worked out for you.
Jul 18 '07 #8
A further question, is there an equivalent command to DMax (shown below) to bring back string to var1

var1 = DMax("[Number]", "Inventory", "[Plant] = '" & var2 & "'") + 1

I hope there is.

Martin
Jul 19 '07 #9
JKing
1,206 Expert 1GB
Could you explain the problem a little more. Is [Number] a text data type? Or is your insert failing because var1 needs to be of text data type?
Jul 19 '07 #10
JKing
1,206 Expert 1GB
I'm assuming the problem is that [Number] is a text data type. For example you have the three numbers 10000,3000, 5 all stored in that field. The number 10000 is obviously the greatest but DMax would return 5 because it processes strings differently.

If this is the case try:
Expand|Select|Wrap|Line Numbers
  1. var1 = DMax("CInt([Number])", "Inventory", "[Plant] = '" & var2 & "'") + 1
  2.  
Jul 19 '07 #11
Sorry JKing, I should have explained more. The code you originally sent me works fine and the insert problem is solved.

What I want to do now is another query to perform an update on the table Inventory in the database, based on the number set with var2. I would like to input var2 via a combobox and get the results back to multiple comboboxes and textboxes so the changes for the update can be made there.

For example, the table Inventory has ten fields, three of them being Number (Number), Plant (String) and Location (String). I can set var2 to be 1000 say and I want to query the database to bring back data for the other fields matching 1000, ie if Number=1000, maybe Plant='TBM' and Location='Mill', and set new variables to equal the results.Thus

varnum = DMax("[Number]", "Inventory", "[Number] = '" & var2 & "'")
varplant = VBACOMMAND("[Plant]", "Inventory", "[Number] = '" & var2 & "'")
varlocation = VBACOMMAND("[Location]", "Inventory", "[Number] = '" & var2 & "'")

Where VBACOMMAND is the missing command for the return of strings so that varplant would = TBM and varlocation would = Mill

Does this make sence. If not I will try and explain further.

Martin.
Jul 19 '07 #12
JKing
1,206 Expert 1GB
I think I understand.
Try this function:
Expand|Select|Wrap|Line Numbers
  1. varplant = Dlookup("[Plant]", "Inventory", "[Number] = '" & var2 & "'") 
  2.  
Jul 19 '07 #13
JKing,

you have done it again. It works a treat. Thanks for getting me out of trouble again.

Regards

Martin.
Jul 19 '07 #14
JKing
1,206 Expert 1GB
Anytime Martin, glad I could help once again.
Jul 19 '07 #15

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

Similar topics

6
by: efgh | last post by:
I'm fairly confident in my knowledge of SQL but I'm stumped with regards with an Insert Into statement in Access 2003. I've got a 4 column table that I've simplified about as much as I can, no...
8
by: Carl | last post by:
Hi, I hope someone can share some of their professional advice and help me out with my embarissing problem concerning an Access INSERT query. I have never attempted to create a table with...
4
by: authorking | last post by:
I use the following code to insert a data record in to a datatable of an access database.But every time I execute the command, there will rise an exception and the insert operation can't be...
7
by: kosta | last post by:
hello! one of my forms communicates with a database, and is supposed to add a row to a table using an Insert statement... however, I get a 'oledb - syntax error' exception... I have double...
1
by: geodev | last post by:
I’m copying data across from an Access database to an MSDE database through the use of some vb.net code. Unfortunately the application crashes when it goes to copy across data, in particular...
6
by: rn5a | last post by:
During registration, users are supposed to enter the following details: First Name, Last Name, EMail, UserName, Password, Confirm Password, Address, City, State, Country, Zip & Phone Number. I am...
10
by: MLH | last post by:
Suppose, in a multi-user environment, you have append query SQL in a VBA procedure that looks like INSERT INTO MyTable... and the next line reads MyVar=DMax("","MyTable... You can never be...
1
by: Zuggy | last post by:
I'm trying to create a registration/login script using Access 2003. I'm using ADOdb to connect through ODBC. <?php // Connects to your Database include('adodb/adodb.inc.php'); # load code...
5
by: DonnaL | last post by:
I'm using Access 2000, but this question likely pertains to any version of Access. Simply put, is there a programmatic way of inserting a new Query in whatever master system table stores these...
2
by: paulmitchell507 | last post by:
I think I am attempting a simple procedure but I just can't figure out the correct syntax. My asp (classic) page runs a SELECT query to obtain dates and ID's from 2 tables uSQL = "SELECT...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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...
0
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...
0
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
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
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,...

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.