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.
14 6416
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
Assuming your field Number is actually a number data type.
You could assign the incremented value to var1 before trying to insert. -
var1 = DMax("[Number]", "Inventory", "[Plant] = '" & var2 & "'") + 1
-
-
appAccess.DoCmd.RunSQL _
-
"(insert into Inventory values (" _
-
& var1 & ", '" _
-
& var2 & "', '" _
-
& var3 & "', '" _
-
& var4 & "', '" _
-
& var5 & "', '" _
-
& var6 & "', '" _
-
& var7 & "', '" _
-
& var8 & "', '" _
-
& var9 & "', '" _
-
& var10 & "', '" _
-
& var11 & "'))"
-
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
Assuming your field Number is actually a number data type.
You could assign the incremented value to var1 before trying to insert. -
var1 = DMax("[Number]", "Inventory", "[Plant] = '" & var2 & "'") + 1
-
-
appAccess.DoCmd.RunSQL _
-
"(insert into Inventory values (" _
-
& var1 & ", '" _
-
& var2 & "', '" _
-
& var3 & "', '" _
-
& var4 & "', '" _
-
& var5 & "', '" _
-
& var6 & "', '" _
-
& var7 & "', '" _
-
& var8 & "', '" _
-
& var9 & "', '" _
-
& var10 & "', '" _
-
& var11 & "'))"
-
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
Yeah just change it to the following:
Yeah just change it to the following:
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.
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.
You're welcome. Glad it worked out for you.
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
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?
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: -
var1 = DMax("CInt([Number])", "Inventory", "[Plant] = '" & var2 & "'") + 1
-
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.
I think I understand.
Try this function: -
varplant = Dlookup("[Plant]", "Inventory", "[Number] = '" & var2 & "'")
-
JKing,
you have done it again. It works a treat. Thanks for getting me out of trouble again.
Regards
Martin.
Anytime Martin, glad I could help once again.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |