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.
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.
Comment