Append query with addtional fields

O

Opal

I am trying to create an append query with two additional fields. The
first is a date field. That I do not have an issue with. The second
field I want to be an autonumber type field. I have
input the following expression into the query:

Expr1: DMax("[TxtIDNumber]+1","ConcernComparetbl")

The "ConcernComparetbl" is where the append query is adding the
records.
However, when I run the query, the number in the Expr1 is the same for
all records.
I can't quite figure out how to increment the numbers in the field.
Can anyone
assist?
 
T

tina

suggest you use a custom function, as

Expr1: isIncrement([MyField])

and put the following function in a standard module, as

Public Function isIncrement(ByVal var As Variant) As Long

isIncrement = DMax("TxtIDNumber", _
"ConcernComparetbl") + 1

End Function

it doesn't matter which field you use in the function argument, as long as
it's a field in the table/query you're appending FROM. the actual field
value is not used in the function; but referring to a field forces the
function to be called for every record that is appended - rather than once
for the whole append action, which is what was happening in your query.

hth
 
O

Opal

suggest you use a custom function, as

Expr1: isIncrement([MyField])

and put the following function in a standard module, as

Public Function isIncrement(ByVal var As Variant) As Long

isIncrement = DMax("TxtIDNumber", _
"ConcernComparetbl") + 1

End Function

it doesn't matter which field you use in the function argument, as long as
it's a field in the table/query you're appending FROM. the actual field
value is not used in the function; but referring to a field forces the
function to be called for every record that is appended - rather than once
for the whole append action, which is what was happening in your query.

hth




I am trying to create an append query with two additional fields. The
first is a date field. That I do not have an issue with. The second
field I want to be an autonumber type field. I have
input the following expression into the query:
Expr1: DMax("[TxtIDNumber]+1","ConcernComparetbl")
The "ConcernComparetbl" is where the append query is adding the
records.
However, when I run the query, the number in the Expr1 is the same for
all records.
I can't quite figure out how to increment the numbers in the field.
Can anyone
assist?- Hide quoted text -

- Show quoted text -

Thank you, Tina...but I get an "Invalid Use of Null" error when I
attempt to use this.....?
 
O

Opal

suggest you use a custom function, as

Expr1: isIncrement([MyField])

and put the following function in a standard module, as

Public Function isIncrement(ByVal var As Variant) As Long

isIncrement = DMax("TxtIDNumber", _
"ConcernComparetbl") + 1

End Function

it doesn't matter which field you use in the function argument, as long as
it's a field in the table/query you're appending FROM. the actual field
value is not used in the function; but referring to a field forces the
function to be called for every record that is appended - rather than once
for the whole append action, which is what was happening in your query.

hth




I am trying to create an append query with two additional fields. The
first is a date field. That I do not have an issue with. The second
field I want to be an autonumber type field. I have
input the following expression into the query:
Expr1: DMax("[TxtIDNumber]+1","ConcernComparetbl")
The "ConcernComparetbl" is where the append query is adding the
records.
However, when I run the query, the number in the Expr1 is the same for
all records.
I can't quite figure out how to increment the numbers in the field.
Can anyone
assist?- Hide quoted text -

- Show quoted text -

I changed the module to:

isIncrement = Nz(DMax("TxtIDNumber", "ConcernComparetbl"), 0) + 1

and that got rid of the Invalid use of Null error, however, the number
for each record does not increment. I have
two records in the query and they are both numbered 1.
 
T

tina

what is the name of the table you're appending TO?

hth


Opal said:
suggest you use a custom function, as

Expr1: isIncrement([MyField])

and put the following function in a standard module, as

Public Function isIncrement(ByVal var As Variant) As Long

isIncrement = DMax("TxtIDNumber", _
"ConcernComparetbl") + 1

End Function

it doesn't matter which field you use in the function argument, as long as
it's a field in the table/query you're appending FROM. the actual field
value is not used in the function; but referring to a field forces the
function to be called for every record that is appended - rather than once
for the whole append action, which is what was happening in your query.

hth




I am trying to create an append query with two additional fields. The
first is a date field. That I do not have an issue with. The second
field I want to be an autonumber type field. I have
input the following expression into the query:
Expr1: DMax("[TxtIDNumber]+1","ConcernComparetbl")
The "ConcernComparetbl" is where the append query is adding the
records.
However, when I run the query, the number in the Expr1 is the same for
all records.
I can't quite figure out how to increment the numbers in the field.
Can anyone
assist?- Hide quoted text -

- Show quoted text -

I changed the module to:

isIncrement = Nz(DMax("TxtIDNumber", "ConcernComparetbl"), 0) + 1

and that got rid of the Invalid use of Null error, however, the number
for each record does not increment. I have
two records in the query and they are both numbered 1.
 
O

Opal

what is the name of the table you're appending TO?

hth




suggest you use a custom function, as
Expr1: isIncrement([MyField])
and put the following function in a standard module, as
Public Function isIncrement(ByVal var As Variant) As Long
isIncrement = DMax("TxtIDNumber", _
"ConcernComparetbl") + 1
End Function
it doesn't matter which field you use in the function argument, as long as
it's a field in the table/query you're appending FROM. the actual field
value is not used in the function; but referring to a field forces the
function to be called for every record that is appended - rather than once
for the whole append action, which is what was happening in your query.
hth

I am trying to create an append query with two additional fields. The
first is a date field. That I do not have an issue with. The second
field I want to be an autonumber type field. I have
input the following expression into the query:
Expr1: DMax("[TxtIDNumber]+1","ConcernComparetbl")
The "ConcernComparetbl" is where the append query is adding the
records.
However, when I run the query, the number in the Expr1 is the same for
all records.
I can't quite figure out how to increment the numbers in the field.
Can anyone
assist?- Hide quoted text -
- Show quoted text -
I changed the module to:
isIncrement = Nz(DMax("TxtIDNumber", "ConcernComparetbl"), 0) + 1
and that got rid of the Invalid use of Null error, however, the number
for each record does not increment. I have
two records in the query and they are both numbered 1.- Hide quoted text -

- Show quoted text -

ConcernComparetbl
 
T

tina

i was afraid of that. my understanding of action queries is that the action
takes place in a transaction, where all record changes are committed at the
same time. so even though the custom function is called for each append
record, the highest value in the table does not actually change until all
the records in the query have been processed, so you're not going to get an
incremented value within the query records.

let's add a couple of global variables to the mix and see if that works.

Public lng As Long
Public bln As Boolean

Public Function isIncrement(ByVal var As Variant) As Long

If bln = False Then
lng = Nz(DMax("TxtIDNumber", _
"ConcernComparetbl"), 0)
bln = True
End If

lng = lng + 1
isIncrement = lng

End Function

add the public variables to the public module, directly below the statements

Option Compare Database
Option Explicit

there are certain risks in using public variables: if you run the Append
query more than once during the same session, and if any unhandled errors
have occurred in other code between one query run and the next, the
variables may be reset. if by chance the lng variable resets, but not the
bln variable, the increment will be incorrect and will probably cause an
error in the Append.

another approach would be to add a table with a single field and one record,
to store the highest number in the TxtIDNumber field of table
ConcernComparetbl, as

tblHighNum
Num (Number, Long Integer)
< add one record to the table, with a value of 0 (zero)>

then you could dispense with the public variables, and use the following
custom function, as

Public Function isIncrement(ByVal var As Variant) As Long

Dim lng as Long

lng = DLookup("Num", "tblHighNum") + 1
CurrentDb.Execute "UPDATE tblHighNum SET Num = " _
& lng
isIncrement = lng

End Function

note that if you're also adding records to table ConcernComparetbl *outside
of the Append query*, then you'll need to update the Num value in tblHighNum
before running the query.

hth


Opal said:
what is the name of the table you're appending TO?

hth




suggest you use a custom function, as
Expr1: isIncrement([MyField])
and put the following function in a standard module, as
Public Function isIncrement(ByVal var As Variant) As Long
isIncrement = DMax("TxtIDNumber", _
"ConcernComparetbl") + 1
End Function
it doesn't matter which field you use in the function argument, as
long
as
it's a field in the table/query you're appending FROM. the actual field
value is not used in the function; but referring to a field forces the
function to be called for every record that is appended - rather
than
once
for the whole append action, which is what was happening in your
query.
I am trying to create an append query with two additional fields. The
first is a date field. That I do not have an issue with. The second
field I want to be an autonumber type field. I have
input the following expression into the query:
Expr1: DMax("[TxtIDNumber]+1","ConcernComparetbl")
The "ConcernComparetbl" is where the append query is adding the
records.
However, when I run the query, the number in the Expr1 is the same for
all records.
I can't quite figure out how to increment the numbers in the field.
Can anyone
assist?- Hide quoted text -
- Show quoted text -
I changed the module to:
isIncrement = Nz(DMax("TxtIDNumber", "ConcernComparetbl"), 0) + 1
and that got rid of the Invalid use of Null error, however, the number
for each record does not increment. I have
two records in the query and they are both numbered 1.- Hide quoted
text -

- Show quoted text -

ConcernComparetbl
 
O

Opal

i was afraid of that. my understanding of action queries is that the action
takes place in a transaction, where all record changes are committed at the
same time. so even though the custom function is called for each append
record, the highest value in the table does not actually change until all
the records in the query have been processed, so you're not going to get an
incremented value within the query records.

let's add a couple of global variables to the mix and see if that works.

Public lng As Long
Public bln As Boolean

Public Function isIncrement(ByVal var As Variant) As Long

If bln = False Then
lng = Nz(DMax("TxtIDNumber", _
"ConcernComparetbl"), 0)
bln = True
End If

lng = lng + 1
isIncrement = lng

End Function

add the public variables to the public module, directly below the statements

Option Compare Database
Option Explicit

there are certain risks in using public variables: if you run the Append
query more than once during the same session, and if any unhandled errors
have occurred in other code between one query run and the next, the
variables may be reset. if by chance the lng variable resets, but not the
bln variable, the increment will be incorrect and will probably cause an
error in the Append.

another approach would be to add a table with a single field and one record,
to store the highest number in the TxtIDNumber field of table
ConcernComparetbl, as

tblHighNum
Num (Number, Long Integer)
< add one record to the table, with a value of 0 (zero)>

then you could dispense with the public variables, and use the following
custom function, as

Public Function isIncrement(ByVal var As Variant) As Long

Dim lng as Long

lng = DLookup("Num", "tblHighNum") + 1
CurrentDb.Execute "UPDATE tblHighNum SET Num = " _
& lng
isIncrement = lng

End Function

note that if you're also adding records to table ConcernComparetbl *outside
of the Append query*, then you'll need to update the Num value in tblHighNum
before running the query.

hth




what is the name of the table you're appending TO?
hth

suggest you use a custom function, as
Expr1: isIncrement([MyField])
and put the following function in a standard module, as
Public Function isIncrement(ByVal var As Variant) As Long
isIncrement = DMax("TxtIDNumber", _
"ConcernComparetbl") + 1
End Function
it doesn't matter which field you use in the function argument, as long
as
it's a field in the table/query you're appending FROM. the actual field
value is not used in the function; but referring to a field forces the
function to be called for every record that is appended - rather than
once
for the whole append action, which is what was happening in your
query.
hth

I am trying to create an append query with two additional fields. The
first is a date field. That I do not have an issue with. The second
field I want to be an autonumber type field. I have
input the following expression into the query:
Expr1: DMax("[TxtIDNumber]+1","ConcernComparetbl")
The "ConcernComparetbl" is where the append query is adding the
records.
However, when I run the query, the number in the Expr1 is the same for
all records.
I can't quite figure out how to increment the numbers in the field.
Can anyone
assist?- Hide quoted text -
- Show quoted text -
I changed the module to:
isIncrement = Nz(DMax("TxtIDNumber", "ConcernComparetbl"), 0) + 1
and that got rid of the Invalid use of Null error, however, the number
for each record does not increment. I have
two records in the query and they are both numbered 1.- Hide quoted
text -
ConcernComparetbl- Hide quoted text -

- Show quoted text -

Thank you Tina....I will try this out.
 
O

Opal

i was afraid of that. my understanding of action queries is that the action
takes place in a transaction, where all record changes are committed at the
same time. so even though the custom function is called for each append
record, the highest value in the table does not actually change until all
the records in the query have been processed, so you're not going to get an
incremented value within the query records.
let's add a couple of global variables to the mix and see if that works.
Public lng As Long
Public bln As Boolean
Public Function isIncrement(ByVal var As Variant) As Long
If bln = False Then
lng = Nz(DMax("TxtIDNumber", _
"ConcernComparetbl"), 0)
bln = True
End If
lng = lng + 1
isIncrement = lng
End Function
add the public variables to the public module, directly below the statements
Option Compare Database
Option Explicit
there are certain risks in using public variables: if you run the Append
query more than once during the same session, and if any unhandled errors
have occurred in other code between one query run and the next, the
variables may be reset. if by chance the lng variable resets, but not the
bln variable, the increment will be incorrect and will probably cause an
error in the Append.
another approach would be to add a table with a single field and one record,
to store the highest number in the TxtIDNumber field of table
ConcernComparetbl, as
tblHighNum
Num (Number, Long Integer)
< add one record to the table, with a value of 0 (zero)>
then you could dispense with the public variables, and use the following
custom function, as
Public Function isIncrement(ByVal var As Variant) As Long
Dim lng as Long
lng = DLookup("Num", "tblHighNum") + 1
CurrentDb.Execute "UPDATE tblHighNum SET Num = " _
& lng
isIncrement = lng
End Function
note that if you're also adding records to table ConcernComparetbl *outside
of the Append query*, then you'll need to update the Num value in tblHighNum
before running the query.

what is the name of the table you're appending TO?
hth

suggest you use a custom function, as
Expr1: isIncrement([MyField])
and put the following function in a standard module, as
Public Function isIncrement(ByVal var As Variant) As Long
isIncrement = DMax("TxtIDNumber", _
"ConcernComparetbl") + 1
End Function
it doesn't matter which field you use in the function argument, as long
as
it's a field in the table/query you're appending FROM. the actual field
value is not used in the function; but referring to a field forces the
function to be called for every record that is appended - rather than
once
for the whole append action, which is what was happening in your query.
hth

I am trying to create an append query with two additional fields. The
first is a date field. That I do not have an issue with. The second
field I want to be an autonumber type field. I have
input the following expression into the query:
Expr1: DMax("[TxtIDNumber]+1","ConcernComparetbl")
The "ConcernComparetbl" is where the append query is adding the
records.
However, when I run the query, the number in the Expr1 is the same for
all records.
I can't quite figure out how to increment the numbers in the field.
Can anyone
assist?- Hide quoted text -
- Show quoted text -
I changed the module to:
isIncrement = Nz(DMax("TxtIDNumber", "ConcernComparetbl"), 0) + 1
and that got rid of the Invalid use of Null error, however, the number
for each record does not increment. I have
two records in the query and they are both numbered 1.- Hide quoted text -
- Show quoted text -
ConcernComparetbl- Hide quoted text -
- Show quoted text -

Thank you Tina....I will try this out.- Hide quoted text -

- Show quoted text -

Interesting results with the public variable option...there is an
increment...but
it jumps all over the place....I have two test records, and they are
added to the table
as 3 and 4, but they are the first records in the table, so they
should be 1 and 2....

When I try the query again, it jumps again ... 23, 24, 27, 28....

I will try the second option as users will not be adding records to
the table outside
of the append query.
 
O

Opal

i was afraid of that. my understanding of action queries is that the action
takes place in a transaction, where all record changes are committed at the
same time. so even though the custom function is called for each append
record, the highest value in the table does not actually change until all
the records in the query have been processed, so you're not going to get an
incremented value within the query records.
let's add a couple of global variables to the mix and see if that works.
Public lng As Long
Public bln As Boolean
Public Function isIncrement(ByVal var As Variant) As Long
If bln = False Then
lng = Nz(DMax("TxtIDNumber", _
"ConcernComparetbl"), 0)
bln = True
End If
lng = lng + 1
isIncrement = lng
End Function
add the public variables to the public module, directly below the statements
Option Compare Database
Option Explicit
there are certain risks in using public variables: if you run the Append
query more than once during the same session, and if any unhandled errors
have occurred in other code between one query run and the next, the
variables may be reset. if by chance the lng variable resets, but not the
bln variable, the increment will be incorrect and will probably cause an
error in the Append.
another approach would be to add a table with a single field and one record,
to store the highest number in the TxtIDNumber field of table
ConcernComparetbl, as
tblHighNum
Num (Number, Long Integer)
< add one record to the table, with a value of 0 (zero)>
then you could dispense with the public variables, and use the following
custom function, as
Public Function isIncrement(ByVal var As Variant) As Long
Dim lng as Long
lng = DLookup("Num", "tblHighNum") + 1
CurrentDb.Execute "UPDATE tblHighNum SET Num = " _
& lng
isIncrement = lng
End Function
note that if you're also adding records to table ConcernComparetbl *outside
of the Append query*, then you'll need to update the Num value in tblHighNum
before running the query.
hth

what is the name of the table you're appending TO?
hth

suggest you use a custom function, as
Expr1: isIncrement([MyField])
and put the following function in a standard module, as
Public Function isIncrement(ByVal var As Variant) As Long
isIncrement = DMax("TxtIDNumber", _
"ConcernComparetbl") + 1
End Function
it doesn't matter which field you use in the function argument, as
long
as
it's a field in the table/query you're appending FROM. the actual
field
value is not used in the function; but referring to a field forces
the
function to be called for every record that is appended - rather
than
once
for the whole append action, which is what was happening in your
query.
hth

I am trying to create an append query with two additional fields.
The
first is a date field. That I do not have an issue with. The
second
field I want to be an autonumber type field. I have
input the following expression into the query:
Expr1: DMax("[TxtIDNumber]+1","ConcernComparetbl")
The "ConcernComparetbl" is where the append query is adding the
records.
However, when I run the query, the number in the Expr1 is the same
for
all records.
I can't quite figure out how to increment the numbers in the
field.
Can anyone
assist?- Hide quoted text -
- Show quoted text -
I changed the module to:
isIncrement = Nz(DMax("TxtIDNumber", "ConcernComparetbl"), 0) + 1
and that got rid of the Invalid use of Null error, however, the number
for each record does not increment. I have
two records in the query and they are both numbered 1.- Hide quoted
text -
- Show quoted text -
ConcernComparetbl- Hide quoted text -
- Show quoted text -
Thank you Tina....I will try this out.- Hide quoted text -
- Show quoted text -

Interesting results with the public variable option...there is an
increment...but
it jumps all over the place....I have two test records, and they are
added to the table
as 3 and 4, but they are the first records in the table, so they
should be 1 and 2....

When I try the query again, it jumps again ... 23, 24, 27, 28....

I will try the second option as users will not be adding records to
the table outside
of the append query.- Hide quoted text -

- Show quoted text -

Hi again, Tina,

Same issue....numbers keep increasing 31, 32, 35, 36
 
T

tina

my only suggestion would be that you put a break in the procedure and then
run the query, stepping through the code to see how many times the function
actually runs - perhaps it's being called more than once for each record,
for some reason.

hth


Opal said:
On Oct 28, 4:33 pm, "tina" <[email protected]> wrote:
i was afraid of that. my understanding of action queries is that the action
takes place in a transaction, where all record changes are committed at the
same time. so even though the custom function is called for each append
record, the highest value in the table does not actually change until all
the records in the query have been processed, so you're not going to get an
incremented value within the query records.
let's add a couple of global variables to the mix and see if that
works.
Public lng As Long
Public bln As Boolean
Public Function isIncrement(ByVal var As Variant) As Long
If bln = False Then
lng = Nz(DMax("TxtIDNumber", _
"ConcernComparetbl"), 0)
bln = True
End If
lng = lng + 1
isIncrement = lng
End Function
add the public variables to the public module, directly below the
statements
Option Compare Database
Option Explicit
there are certain risks in using public variables: if you run the Append
query more than once during the same session, and if any unhandled errors
have occurred in other code between one query run and the next, the
variables may be reset. if by chance the lng variable resets, but not the
bln variable, the increment will be incorrect and will probably cause an
error in the Append.
another approach would be to add a table with a single field and one record,
to store the highest number in the TxtIDNumber field of table
ConcernComparetbl, as
tblHighNum
Num (Number, Long Integer)
< add one record to the table, with a value of 0 (zero)>
then you could dispense with the public variables, and use the following
custom function, as
Public Function isIncrement(ByVal var As Variant) As Long
Dim lng as Long
lng = DLookup("Num", "tblHighNum") + 1
CurrentDb.Execute "UPDATE tblHighNum SET Num = " _
& lng
isIncrement = lng
End Function
note that if you're also adding records to table ConcernComparetbl *outside
of the Append query*, then you'll need to update the Num value in tblHighNum
before running the query.



what is the name of the table you're appending TO?



suggest you use a custom function, as
Expr1: isIncrement([MyField])
and put the following function in a standard module, as
Public Function isIncrement(ByVal var As Variant) As Long
isIncrement = DMax("TxtIDNumber", _
"ConcernComparetbl") + 1
End Function
it doesn't matter which field you use in the function argument, as
long
as
it's a field in the table/query you're appending FROM. the actual
field
value is not used in the function; but referring to a field forces
the
function to be called for every record that is appended - rather
than
once
for the whole append action, which is what was happening in your
query.



I am trying to create an append query with two additional fields.
The
first is a date field. That I do not have an issue with. The
second
field I want to be an autonumber type field. I have
input the following expression into the query:
Expr1: DMax("[TxtIDNumber]+1","ConcernComparetbl")
The "ConcernComparetbl" is where the append query is adding the
records.
However, when I run the query, the number in the Expr1 is the same
for
all records.
I can't quite figure out how to increment the numbers in the
field.
Can anyone
assist?- Hide quoted text -
- Show quoted text -
I changed the module to:
isIncrement = Nz(DMax("TxtIDNumber", "ConcernComparetbl"), 0)
+ 1
and that got rid of the Invalid use of Null error, however, the number
for each record does not increment. I have
two records in the query and they are both numbered 1.- Hide quoted
text -
- Show quoted text -
ConcernComparetbl- Hide quoted text -
- Show quoted text -
Thank you Tina....I will try this out.- Hide quoted text -
- Show quoted text -

Interesting results with the public variable option...there is an
increment...but
it jumps all over the place....I have two test records, and they are
added to the table
as 3 and 4, but they are the first records in the table, so they
should be 1 and 2....

When I try the query again, it jumps again ... 23, 24, 27, 28....

I will try the second option as users will not be adding records to
the table outside
of the append query.- Hide quoted text -

- Show quoted text -

Hi again, Tina,

Same issue....numbers keep increasing 31, 32, 35, 36
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top