duplicating control and incrementing another control

G

Guest

i have an a2k application and a form having two controls which make up the PK.
a) Patient Number
b) Lesion Number

the form is 'filtered' so when user designates a patient number to work on
from another open form, he's taken to this one and can view only those PNs
selected.

there are multiple records viewable on this form (it's coninuous) each one
having an incrementally larger value of "Lestion Number" (from 1,2,....,12)

i wrote a few vba pieces of code shown below

Private Sub Form_Load()
LAS_EnableSecurity Me
Me.Patient_Number.Value = Forms![RECIST Disease Evaluation: Nontarget
Lesions]![Patient Number]
End Sub

the above to handle the case when the form's loading (and was written for
the case when no records were entered prior)

how can i let user simultaneously duplicate a Patient Number and increment a
Lesion Number w/o having to do so manually.

Private Sub Patient_Number_AfterUpdate()
Me.Patient_Number.DefaultValue = "'" & [Patient Number] & "'" ' that's a
single quote within doubles...
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1 + DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = " & Me![Patient Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the upper limit",
vbInformation)
End If
DoCmd.Requery
End Sub

the above intended to duplicate the value of Patient Number and increment
Lesion Number.

in testing this out when no other records matching the PN were created, i
opened the form and found Patient Number filled in (by the 'load' vba
presumably).

the 'pencil' is on the first record and the Patient Number's completed. the
'*' is on the record below. if i manually key in '01' into the Lesion Number
control it returns a run-time error '3058' abut index or pk not being able
to contain a null -value and highligts the DoCmd.Requery in the vba code below

Private Sub Lesion_Number_AfterUpdate()
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1 + DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = " & Me![Patient Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the upper limit",
vbInformation)
End If
DoCmd.Requery
End Sub


this is probably one of those 'really simple' solutions that befuddle the
newbie population.


but to further round things out, if i open a form with a bunch of records
already entered and click on 'add record'. i need to manually key in the PN
when i click 'add record' at which instant it automatically fills in the LN,
displays a record (w/ completed PN in the '*' row) and causes the cursor to
jump to the topmost (first record).

????
 
R

Rob Oldfield

When you click the 'New Record' button on the standard record navigation bar
then, if the form is filtered, it doesn't automatically carry that filtering
value on to the new record.

Where is the form opened from? It should (always) be another form (or
forms) that specify the patient number. I'd tend to use code on that (or
those) form(s) that adds a new record via an SQL statement (including the PN
and the dmax+1) and then just go to that record.


Ted said:
i have an a2k application and a form having two controls which make up the PK.
a) Patient Number
b) Lesion Number

the form is 'filtered' so when user designates a patient number to work on
from another open form, he's taken to this one and can view only those PNs
selected.

there are multiple records viewable on this form (it's coninuous) each one
having an incrementally larger value of "Lestion Number" (from 1,2,....,12)

i wrote a few vba pieces of code shown below

Private Sub Form_Load()
LAS_EnableSecurity Me
Me.Patient_Number.Value = Forms![RECIST Disease Evaluation: Nontarget
Lesions]![Patient Number]
End Sub

the above to handle the case when the form's loading (and was written for
the case when no records were entered prior)

how can i let user simultaneously duplicate a Patient Number and increment a
Lesion Number w/o having to do so manually.

Private Sub Patient_Number_AfterUpdate()
Me.Patient_Number.DefaultValue = "'" & [Patient Number] & "'" ' that's a
single quote within doubles...
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1 + DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = " & Me![Patient Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the upper limit",
vbInformation)
End If
DoCmd.Requery
End Sub

the above intended to duplicate the value of Patient Number and increment
Lesion Number.

in testing this out when no other records matching the PN were created, i
opened the form and found Patient Number filled in (by the 'load' vba
presumably).

the 'pencil' is on the first record and the Patient Number's completed. the
'*' is on the record below. if i manually key in '01' into the Lesion Number
control it returns a run-time error '3058' abut index or pk not being able
to contain a null -value and highligts the DoCmd.Requery in the vba code below

Private Sub Lesion_Number_AfterUpdate()
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1 + DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = " & Me![Patient Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the upper limit",
vbInformation)
End If
DoCmd.Requery
End Sub


this is probably one of those 'really simple' solutions that befuddle the
newbie population.


but to further round things out, if i open a form with a bunch of records
already entered and click on 'add record'. i need to manually key in the PN
when i click 'add record' at which instant it automatically fills in the LN,
displays a record (w/ completed PN in the '*' row) and causes the cursor to
jump to the topmost (first record).

????
 
G

Guest

i've been working on this today...now what i'm trying is as follows:

when a new record's to be added to the form, i use a default expression of

=[Forms]![RECIST Disease Evaluation: Nontarget Lesions]![Patient Number]

which seems to work nicely so far thank you very much.

when we get to "Lesion Number" the defalut expression i used was

=IIf(IsNull("[Lesion Number]"),1,1+DMax("[Lesion Number]","Lesions:
Non-Target","[Patient Number] = " & [Me]![Patient Number]))

and that seems to be populating the form's control field with "#Name?".

not exactly what i'd had in mind: i.e. the first time this record gets
completed we'd expect there to be nothing in the "Lesion Number" field so the
sequence would start with the integer value '1'. the next time and every
other when a new record got added via the form we'd expect the Lesion Number
to be incrementally larger by a factor of 1.

so what's up with that?


Rob Oldfield said:
When you click the 'New Record' button on the standard record navigation bar
then, if the form is filtered, it doesn't automatically carry that filtering
value on to the new record.

Where is the form opened from? It should (always) be another form (or
forms) that specify the patient number. I'd tend to use code on that (or
those) form(s) that adds a new record via an SQL statement (including the PN
and the dmax+1) and then just go to that record.


Ted said:
i have an a2k application and a form having two controls which make up the PK.
a) Patient Number
b) Lesion Number

the form is 'filtered' so when user designates a patient number to work on
from another open form, he's taken to this one and can view only those PNs
selected.

there are multiple records viewable on this form (it's coninuous) each one
having an incrementally larger value of "Lestion Number" (from 1,2,....,12)

i wrote a few vba pieces of code shown below

Private Sub Form_Load()
LAS_EnableSecurity Me
Me.Patient_Number.Value = Forms![RECIST Disease Evaluation: Nontarget
Lesions]![Patient Number]
End Sub

the above to handle the case when the form's loading (and was written for
the case when no records were entered prior)

how can i let user simultaneously duplicate a Patient Number and increment a
Lesion Number w/o having to do so manually.

Private Sub Patient_Number_AfterUpdate()
Me.Patient_Number.DefaultValue = "'" & [Patient Number] & "'" ' that's a
single quote within doubles...
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1 + DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = " & Me![Patient Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the upper limit",
vbInformation)
End If
DoCmd.Requery
End Sub

the above intended to duplicate the value of Patient Number and increment
Lesion Number.

in testing this out when no other records matching the PN were created, i
opened the form and found Patient Number filled in (by the 'load' vba
presumably).

the 'pencil' is on the first record and the Patient Number's completed. the
'*' is on the record below. if i manually key in '01' into the Lesion Number
control it returns a run-time error '3058' abut index or pk not being able
to contain a null -value and highligts the DoCmd.Requery in the vba code below

Private Sub Lesion_Number_AfterUpdate()
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1 + DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = " & Me![Patient Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the upper limit",
vbInformation)
End If
DoCmd.Requery
End Sub


this is probably one of those 'really simple' solutions that befuddle the
newbie population.


but to further round things out, if i open a form with a bunch of records
already entered and click on 'add record'. i need to manually key in the PN
when i click 'add record' at which instant it automatically fills in the LN,
displays a record (w/ completed PN in the '*' row) and causes the cursor to
jump to the topmost (first record).

????
 
G

Guest

......i forgot to propose an example of the code you had in mind (assuming the
idea of setting expressions on the Default properties of the Patient and
Lesion Number fields isn't viable).

-ted

Rob Oldfield said:
When you click the 'New Record' button on the standard record navigation bar
then, if the form is filtered, it doesn't automatically carry that filtering
value on to the new record.

Where is the form opened from? It should (always) be another form (or
forms) that specify the patient number. I'd tend to use code on that (or
those) form(s) that adds a new record via an SQL statement (including the PN
and the dmax+1) and then just go to that record.


Ted said:
i have an a2k application and a form having two controls which make up the PK.
a) Patient Number
b) Lesion Number

the form is 'filtered' so when user designates a patient number to work on
from another open form, he's taken to this one and can view only those PNs
selected.

there are multiple records viewable on this form (it's coninuous) each one
having an incrementally larger value of "Lestion Number" (from 1,2,....,12)

i wrote a few vba pieces of code shown below

Private Sub Form_Load()
LAS_EnableSecurity Me
Me.Patient_Number.Value = Forms![RECIST Disease Evaluation: Nontarget
Lesions]![Patient Number]
End Sub

the above to handle the case when the form's loading (and was written for
the case when no records were entered prior)

how can i let user simultaneously duplicate a Patient Number and increment a
Lesion Number w/o having to do so manually.

Private Sub Patient_Number_AfterUpdate()
Me.Patient_Number.DefaultValue = "'" & [Patient Number] & "'" ' that's a
single quote within doubles...
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1 + DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = " & Me![Patient Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the upper limit",
vbInformation)
End If
DoCmd.Requery
End Sub

the above intended to duplicate the value of Patient Number and increment
Lesion Number.

in testing this out when no other records matching the PN were created, i
opened the form and found Patient Number filled in (by the 'load' vba
presumably).

the 'pencil' is on the first record and the Patient Number's completed. the
'*' is on the record below. if i manually key in '01' into the Lesion Number
control it returns a run-time error '3058' abut index or pk not being able
to contain a null -value and highligts the DoCmd.Requery in the vba code below

Private Sub Lesion_Number_AfterUpdate()
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1 + DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = " & Me![Patient Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the upper limit",
vbInformation)
End If
DoCmd.Requery
End Sub


this is probably one of those 'really simple' solutions that befuddle the
newbie population.


but to further round things out, if i open a form with a bunch of records
already entered and click on 'add record'. i need to manually key in the PN
when i click 'add record' at which instant it automatically fills in the LN,
displays a record (w/ completed PN in the '*' row) and causes the cursor to
jump to the topmost (first record).

????
 
R

Rob Oldfield

It's viable... just a bit flaky and difficult to control (as I think you've
found). Your expression [Me]![Patient Number] in your dmax will only work
if the record has already been added and that leads to timing issues which
is where I think your problem is.

If you want a record with a specific PN and lesion number then you're better
off writing it directly. You get way more control and debugability.


Ted said:
.....i forgot to propose an example of the code you had in mind (assuming the
idea of setting expressions on the Default properties of the Patient and
Lesion Number fields isn't viable).

-ted

Rob Oldfield said:
When you click the 'New Record' button on the standard record navigation bar
then, if the form is filtered, it doesn't automatically carry that filtering
value on to the new record.

Where is the form opened from? It should (always) be another form (or
forms) that specify the patient number. I'd tend to use code on that (or
those) form(s) that adds a new record via an SQL statement (including the PN
and the dmax+1) and then just go to that record.


Ted said:
i have an a2k application and a form having two controls which make up
the
PK.
a) Patient Number
b) Lesion Number

the form is 'filtered' so when user designates a patient number to work on
from another open form, he's taken to this one and can view only those PNs
selected.

there are multiple records viewable on this form (it's coninuous) each one
having an incrementally larger value of "Lestion Number" (from 1,2,....,12)

i wrote a few vba pieces of code shown below

Private Sub Form_Load()
LAS_EnableSecurity Me
Me.Patient_Number.Value = Forms![RECIST Disease Evaluation: Nontarget
Lesions]![Patient Number]
End Sub

the above to handle the case when the form's loading (and was written for
the case when no records were entered prior)

how can i let user simultaneously duplicate a Patient Number and
increment
a
Lesion Number w/o having to do so manually.

Private Sub Patient_Number_AfterUpdate()
Me.Patient_Number.DefaultValue = "'" & [Patient Number] & "'" '
that's
a
single quote within doubles...
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1 + DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = " & Me![Patient Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the upper limit",
vbInformation)
End If
DoCmd.Requery
End Sub

the above intended to duplicate the value of Patient Number and increment
Lesion Number.

in testing this out when no other records matching the PN were created, i
opened the form and found Patient Number filled in (by the 'load' vba
presumably).

the 'pencil' is on the first record and the Patient Number's
completed.
the
'*' is on the record below. if i manually key in '01' into the Lesion Number
control it returns a run-time error '3058' abut index or pk not being able
to contain a null -value and highligts the DoCmd.Requery in the vba
code
below
Private Sub Lesion_Number_AfterUpdate()
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1 + DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = " & Me![Patient Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the upper limit",
vbInformation)
End If
DoCmd.Requery
End Sub


this is probably one of those 'really simple' solutions that befuddle the
newbie population.


but to further round things out, if i open a form with a bunch of records
already entered and click on 'add record'. i need to manually key in
the
PN
when i click 'add record' at which instant it automatically fills in
the
LN,
displays a record (w/ completed PN in the '*' row) and causes the
cursor
to
jump to the topmost (first record).

????
 
G

Guest

since i don't pretend to be the final word on vba (what would i be doing
asking all this stuff), what i think you're saying is that it won't work on
the Default Property of Lesion Number no matter how much i adhere to the
tenets of the other contributors to my posting....

you mentioned that you thought some SQL-izing would be constructive. can you
flesh that out a little. what are you thinking about....remember this is a
newbie on this end :)



Rob Oldfield said:
It's viable... just a bit flaky and difficult to control (as I think you've
found). Your expression [Me]![Patient Number] in your dmax will only work
if the record has already been added and that leads to timing issues which
is where I think your problem is.

If you want a record with a specific PN and lesion number then you're better
off writing it directly. You get way more control and debugability.


Ted said:
.....i forgot to propose an example of the code you had in mind (assuming the
idea of setting expressions on the Default properties of the Patient and
Lesion Number fields isn't viable).

-ted

Rob Oldfield said:
When you click the 'New Record' button on the standard record navigation bar
then, if the form is filtered, it doesn't automatically carry that filtering
value on to the new record.

Where is the form opened from? It should (always) be another form (or
forms) that specify the patient number. I'd tend to use code on that (or
those) form(s) that adds a new record via an SQL statement (including the PN
and the dmax+1) and then just go to that record.


i have an a2k application and a form having two controls which make up the
PK.
a) Patient Number
b) Lesion Number

the form is 'filtered' so when user designates a patient number to work on
from another open form, he's taken to this one and can view only those PNs
selected.

there are multiple records viewable on this form (it's coninuous) each one
having an incrementally larger value of "Lestion Number" (from
1,2,....,12)

i wrote a few vba pieces of code shown below

Private Sub Form_Load()
LAS_EnableSecurity Me
Me.Patient_Number.Value = Forms![RECIST Disease Evaluation: Nontarget
Lesions]![Patient Number]
End Sub

the above to handle the case when the form's loading (and was written for
the case when no records were entered prior)

how can i let user simultaneously duplicate a Patient Number and increment
a
Lesion Number w/o having to do so manually.

Private Sub Patient_Number_AfterUpdate()
Me.Patient_Number.DefaultValue = "'" & [Patient Number] & "'" ' that's
a
single quote within doubles...
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1 + DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = " & Me![Patient
Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the upper limit",
vbInformation)
End If
DoCmd.Requery
End Sub

the above intended to duplicate the value of Patient Number and increment
Lesion Number.

in testing this out when no other records matching the PN were created, i
opened the form and found Patient Number filled in (by the 'load' vba
presumably).

the 'pencil' is on the first record and the Patient Number's completed.
the
'*' is on the record below. if i manually key in '01' into the Lesion
Number
control it returns a run-time error '3058' abut index or pk not being
able
to contain a null -value and highligts the DoCmd.Requery in the vba code
below

Private Sub Lesion_Number_AfterUpdate()
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1 + DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = " & Me![Patient
Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the upper limit",
vbInformation)
End If
DoCmd.Requery
End Sub


this is probably one of those 'really simple' solutions that befuddle the
newbie population.


but to further round things out, if i open a form with a bunch of records
already entered and click on 'add record'. i need to manually key in the
PN
when i click 'add record' at which instant it automatically fills in the
LN,
displays a record (w/ completed PN in the '*' row) and causes the cursor
to
jump to the topmost (first record).

????
 
R

Rob Oldfield

I'm not saying the default value method will never work... just that it
isn't the best way. Anyway - the SQL method:

Set up a new append query (not based on any table/query) that will add some
values into the table your data would be adding to. Use any values in that
query. Then switch to the SQL view of that query (via the View menu). It
will look something like...

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

You now need to build that same string but instead of the random values 1
and 2... include the actual values you want to use. That will be something
like...

dim sql as string
dim pn as long
dim ln as long
pn=.... 'whatever.. set it to the patient number you want
ln=.... 'whatever... set it to the lesion number you want
sql="INSERT INTO tblTarget ( [Patient Number], [Lesion Number] ) "+ _
"SELECT "+cstr(pn)+" AS PN, "+cstr(ln)+" AS LN;"

Doing it that way you can check that pn and ln are getting calculated
correctly, and by adding a breakpoint, check that the SQL statement
works.... by copying and pasting it into a new query window.

You can then run it by...

dim db as database
set db=currentdb
db.execute sql
set db=nothing

The last part will be just to move to that newly added record. But I
wouldn't worry about that until you get the above working.


Ted said:
since i don't pretend to be the final word on vba (what would i be doing
asking all this stuff), what i think you're saying is that it won't work on
the Default Property of Lesion Number no matter how much i adhere to the
tenets of the other contributors to my posting....

you mentioned that you thought some SQL-izing would be constructive. can you
flesh that out a little. what are you thinking about....remember this is a
newbie on this end :)



Rob Oldfield said:
It's viable... just a bit flaky and difficult to control (as I think you've
found). Your expression [Me]![Patient Number] in your dmax will only work
if the record has already been added and that leads to timing issues which
is where I think your problem is.

If you want a record with a specific PN and lesion number then you're better
off writing it directly. You get way more control and debugability.


Ted said:
.....i forgot to propose an example of the code you had in mind
(assuming
the
idea of setting expressions on the Default properties of the Patient and
Lesion Number fields isn't viable).

-ted

:

When you click the 'New Record' button on the standard record
navigation
bar
then, if the form is filtered, it doesn't automatically carry that filtering
value on to the new record.

Where is the form opened from? It should (always) be another form (or
forms) that specify the patient number. I'd tend to use code on
that
(or
those) form(s) that adds a new record via an SQL statement
(including
the PN
and the dmax+1) and then just go to that record.


i have an a2k application and a form having two controls which
make up
the
PK.
a) Patient Number
b) Lesion Number

the form is 'filtered' so when user designates a patient number to work on
from another open form, he's taken to this one and can view only
those
PNs
selected.

there are multiple records viewable on this form (it's coninuous)
each
one
having an incrementally larger value of "Lestion Number" (from
1,2,....,12)

i wrote a few vba pieces of code shown below

Private Sub Form_Load()
LAS_EnableSecurity Me
Me.Patient_Number.Value = Forms![RECIST Disease Evaluation: Nontarget
Lesions]![Patient Number]
End Sub

the above to handle the case when the form's loading (and was
written
for
the case when no records were entered prior)

how can i let user simultaneously duplicate a Patient Number and increment
a
Lesion Number w/o having to do so manually.

Private Sub Patient_Number_AfterUpdate()
Me.Patient_Number.DefaultValue = "'" & [Patient Number] & "'" ' that's
a
single quote within doubles...
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1 + DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = " & Me![Patient
Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the upper limit",
vbInformation)
End If
DoCmd.Requery
End Sub

the above intended to duplicate the value of Patient Number and increment
Lesion Number.

in testing this out when no other records matching the PN were created, i
opened the form and found Patient Number filled in (by the 'load' vba
presumably).

the 'pencil' is on the first record and the Patient Number's completed.
the
'*' is on the record below. if i manually key in '01' into the Lesion
Number
control it returns a run-time error '3058' abut index or pk not being
able
to contain a null -value and highligts the DoCmd.Requery in the
vba
code
below

Private Sub Lesion_Number_AfterUpdate()
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1 + DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = " & Me![Patient
Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the upper limit",
vbInformation)
End If
DoCmd.Requery
End Sub


this is probably one of those 'really simple' solutions that
befuddle
the
newbie population.


but to further round things out, if i open a form with a bunch of records
already entered and click on 'add record'. i need to manually key
in
the
PN
when i click 'add record' at which instant it automatically fills
in
the
LN,
displays a record (w/ completed PN in the '*' row) and causes the cursor
to
jump to the topmost (first record).

????
 
G

Guest

in addition to the pn and ln (which comprise the PK), there are five other
controls on this same form. do they need referring to in the append query?
does it matter at all that both pn and ln are comboboxes; do they need to get
changed into textboxes?

i still think that the "Default" 'method' is the simpler conceptually to
understand and the one that 'feels' the 'best'.

Rob Oldfield said:
I'm not saying the default value method will never work... just that it
isn't the best way. Anyway - the SQL method:

Set up a new append query (not based on any table/query) that will add some
values into the table your data would be adding to. Use any values in that
query. Then switch to the SQL view of that query (via the View menu). It
will look something like...

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

You now need to build that same string but instead of the random values 1
and 2... include the actual values you want to use. That will be something
like...

dim sql as string
dim pn as long
dim ln as long
pn=.... 'whatever.. set it to the patient number you want
ln=.... 'whatever... set it to the lesion number you want
sql="INSERT INTO tblTarget ( [Patient Number], [Lesion Number] ) "+ _
"SELECT "+cstr(pn)+" AS PN, "+cstr(ln)+" AS LN;"

Doing it that way you can check that pn and ln are getting calculated
correctly, and by adding a breakpoint, check that the SQL statement
works.... by copying and pasting it into a new query window.

You can then run it by...

dim db as database
set db=currentdb
db.execute sql
set db=nothing

The last part will be just to move to that newly added record. But I
wouldn't worry about that until you get the above working.


Ted said:
since i don't pretend to be the final word on vba (what would i be doing
asking all this stuff), what i think you're saying is that it won't work on
the Default Property of Lesion Number no matter how much i adhere to the
tenets of the other contributors to my posting....

you mentioned that you thought some SQL-izing would be constructive. can you
flesh that out a little. what are you thinking about....remember this is a
newbie on this end :)



Rob Oldfield said:
It's viable... just a bit flaky and difficult to control (as I think you've
found). Your expression [Me]![Patient Number] in your dmax will only work
if the record has already been added and that leads to timing issues which
is where I think your problem is.

If you want a record with a specific PN and lesion number then you're better
off writing it directly. You get way more control and debugability.


.....i forgot to propose an example of the code you had in mind (assuming
the
idea of setting expressions on the Default properties of the Patient and
Lesion Number fields isn't viable).

-ted

:

When you click the 'New Record' button on the standard record navigation
bar
then, if the form is filtered, it doesn't automatically carry that
filtering
value on to the new record.

Where is the form opened from? It should (always) be another form (or
forms) that specify the patient number. I'd tend to use code on that
(or
those) form(s) that adds a new record via an SQL statement (including
the PN
and the dmax+1) and then just go to that record.


i have an a2k application and a form having two controls which make up
the
PK.
a) Patient Number
b) Lesion Number

the form is 'filtered' so when user designates a patient number to
work on
from another open form, he's taken to this one and can view only those
PNs
selected.

there are multiple records viewable on this form (it's coninuous) each
one
having an incrementally larger value of "Lestion Number" (from
1,2,....,12)

i wrote a few vba pieces of code shown below

Private Sub Form_Load()
LAS_EnableSecurity Me
Me.Patient_Number.Value = Forms![RECIST Disease Evaluation: Nontarget
Lesions]![Patient Number]
End Sub

the above to handle the case when the form's loading (and was written
for
the case when no records were entered prior)

how can i let user simultaneously duplicate a Patient Number and
increment
a
Lesion Number w/o having to do so manually.

Private Sub Patient_Number_AfterUpdate()
Me.Patient_Number.DefaultValue = "'" & [Patient Number] & "'" '
that's
a
single quote within doubles...
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1 + DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = " & Me![Patient
Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the upper limit",
vbInformation)
End If
DoCmd.Requery
End Sub

the above intended to duplicate the value of Patient Number and
increment
Lesion Number.

in testing this out when no other records matching the PN were
created, i
opened the form and found Patient Number filled in (by the 'load' vba
presumably).

the 'pencil' is on the first record and the Patient Number's
completed.
the
'*' is on the record below. if i manually key in '01' into the Lesion
Number
control it returns a run-time error '3058' abut index or pk not being
able
to contain a null -value and highligts the DoCmd.Requery in the vba
code
below

Private Sub Lesion_Number_AfterUpdate()
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1 + DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = " & Me![Patient
Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the upper limit",
vbInformation)
End If
DoCmd.Requery
End Sub


this is probably one of those 'really simple' solutions that befuddle
the
newbie population.


but to further round things out, if i open a form with a bunch of
records
already entered and click on 'add record'. i need to manually key in
the
PN
when i click 'add record' at which instant it automatically fills in
the
LN,
displays a record (w/ completed PN in the '*' row) and causes the
cursor
to
jump to the topmost (first record).

????
 
R

Rob Oldfield

No. Provided that the new record needs to be written with null values for
any of the other values (or 0s for numeric fields) then just leaving them
out of the query will work fine. The actual format of how you choose to
display those fields on a form is also immaterial - bear in mind that you're
not writing data to the form - that's just a means of viewing the data -
it's going into a table.

Which of the methods you feel is simpler is always going to be down to you.
But I guarantee you 100% that the SQL method is a hugely powerful technique
that has uses in many places in Access. It *is* much better and as I
mentioned before, a great deal easier to debug.


Ted said:
in addition to the pn and ln (which comprise the PK), there are five other
controls on this same form. do they need referring to in the append query?
does it matter at all that both pn and ln are comboboxes; do they need to get
changed into textboxes?

i still think that the "Default" 'method' is the simpler conceptually to
understand and the one that 'feels' the 'best'.

Rob Oldfield said:
I'm not saying the default value method will never work... just that it
isn't the best way. Anyway - the SQL method:

Set up a new append query (not based on any table/query) that will add some
values into the table your data would be adding to. Use any values in that
query. Then switch to the SQL view of that query (via the View menu). It
will look something like...

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

You now need to build that same string but instead of the random values 1
and 2... include the actual values you want to use. That will be something
like...

dim sql as string
dim pn as long
dim ln as long
pn=.... 'whatever.. set it to the patient number you want
ln=.... 'whatever... set it to the lesion number you want
sql="INSERT INTO tblTarget ( [Patient Number], [Lesion Number] ) "+ _
"SELECT "+cstr(pn)+" AS PN, "+cstr(ln)+" AS LN;"

Doing it that way you can check that pn and ln are getting calculated
correctly, and by adding a breakpoint, check that the SQL statement
works.... by copying and pasting it into a new query window.

You can then run it by...

dim db as database
set db=currentdb
db.execute sql
set db=nothing

The last part will be just to move to that newly added record. But I
wouldn't worry about that until you get the above working.


Ted said:
since i don't pretend to be the final word on vba (what would i be doing
asking all this stuff), what i think you're saying is that it won't
work
on
the Default Property of Lesion Number no matter how much i adhere to the
tenets of the other contributors to my posting....

you mentioned that you thought some SQL-izing would be constructive.
can
you
flesh that out a little. what are you thinking about....remember this is a
newbie on this end :)



:

It's viable... just a bit flaky and difficult to control (as I think you've
found). Your expression [Me]![Patient Number] in your dmax will
only
work
if the record has already been added and that leads to timing issues which
is where I think your problem is.

If you want a record with a specific PN and lesion number then
you're
better
off writing it directly. You get way more control and debugability.


.....i forgot to propose an example of the code you had in mind (assuming
the
idea of setting expressions on the Default properties of the
Patient
and
Lesion Number fields isn't viable).

-ted

:

When you click the 'New Record' button on the standard record navigation
bar
then, if the form is filtered, it doesn't automatically carry that
filtering
value on to the new record.

Where is the form opened from? It should (always) be another
form
(or
forms) that specify the patient number. I'd tend to use code on that
(or
those) form(s) that adds a new record via an SQL statement (including
the PN
and the dmax+1) and then just go to that record.


i have an a2k application and a form having two controls which make up
the
PK.
a) Patient Number
b) Lesion Number

the form is 'filtered' so when user designates a patient number to
work on
from another open form, he's taken to this one and can view
only
those
PNs
selected.

there are multiple records viewable on this form (it's
coninuous)
each
one
having an incrementally larger value of "Lestion Number" (from
1,2,....,12)

i wrote a few vba pieces of code shown below

Private Sub Form_Load()
LAS_EnableSecurity Me
Me.Patient_Number.Value = Forms![RECIST Disease Evaluation: Nontarget
Lesions]![Patient Number]
End Sub

the above to handle the case when the form's loading (and was written
for
the case when no records were entered prior)

how can i let user simultaneously duplicate a Patient Number and
increment
a
Lesion Number w/o having to do so manually.

Private Sub Patient_Number_AfterUpdate()
Me.Patient_Number.DefaultValue = "'" & [Patient Number] & "'" '
that's
a
single quote within doubles...
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1 + DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = " & Me![Patient
Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the upper limit",
vbInformation)
End If
DoCmd.Requery
End Sub

the above intended to duplicate the value of Patient Number and
increment
Lesion Number.

in testing this out when no other records matching the PN were
created, i
opened the form and found Patient Number filled in (by the
'load'
vba
presumably).

the 'pencil' is on the first record and the Patient Number's
completed.
the
'*' is on the record below. if i manually key in '01' into the Lesion
Number
control it returns a run-time error '3058' abut index or pk
not
being
able
to contain a null -value and highligts the DoCmd.Requery in
the
vba
code
below

Private Sub Lesion_Number_AfterUpdate()
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1 + DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = " & Me![Patient
Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the upper limit",
vbInformation)
End If
DoCmd.Requery
End Sub


this is probably one of those 'really simple' solutions that befuddle
the
newbie population.


but to further round things out, if i open a form with a bunch of
records
already entered and click on 'add record'. i need to manually
key
in
the
PN
when i click 'add record' at which instant it automatically
fills
in
the
LN,
displays a record (w/ completed PN in the '*' row) and causes the
cursor
to
jump to the topmost (first record).

????
 
G

Guest

okay -- it's probably worth trying it out (the default method isn't working
out for some reason anyway).

where does this code belong?

dim db as database
set db=currentdb
db.execute sql
set db=nothing

best,

-ted



Rob Oldfield said:
No. Provided that the new record needs to be written with null values for
any of the other values (or 0s for numeric fields) then just leaving them
out of the query will work fine. The actual format of how you choose to
display those fields on a form is also immaterial - bear in mind that you're
not writing data to the form - that's just a means of viewing the data -
it's going into a table.

Which of the methods you feel is simpler is always going to be down to you.
But I guarantee you 100% that the SQL method is a hugely powerful technique
that has uses in many places in Access. It *is* much better and as I
mentioned before, a great deal easier to debug.


Ted said:
in addition to the pn and ln (which comprise the PK), there are five other
controls on this same form. do they need referring to in the append query?
does it matter at all that both pn and ln are comboboxes; do they need to get
changed into textboxes?

i still think that the "Default" 'method' is the simpler conceptually to
understand and the one that 'feels' the 'best'.

Rob Oldfield said:
I'm not saying the default value method will never work... just that it
isn't the best way. Anyway - the SQL method:

Set up a new append query (not based on any table/query) that will add some
values into the table your data would be adding to. Use any values in that
query. Then switch to the SQL view of that query (via the View menu). It
will look something like...

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

You now need to build that same string but instead of the random values 1
and 2... include the actual values you want to use. That will be something
like...

dim sql as string
dim pn as long
dim ln as long
pn=.... 'whatever.. set it to the patient number you want
ln=.... 'whatever... set it to the lesion number you want
sql="INSERT INTO tblTarget ( [Patient Number], [Lesion Number] ) "+ _
"SELECT "+cstr(pn)+" AS PN, "+cstr(ln)+" AS LN;"

Doing it that way you can check that pn and ln are getting calculated
correctly, and by adding a breakpoint, check that the SQL statement
works.... by copying and pasting it into a new query window.

You can then run it by...

dim db as database
set db=currentdb
db.execute sql
set db=nothing

The last part will be just to move to that newly added record. But I
wouldn't worry about that until you get the above working.


since i don't pretend to be the final word on vba (what would i be doing
asking all this stuff), what i think you're saying is that it won't work
on
the Default Property of Lesion Number no matter how much i adhere to the
tenets of the other contributors to my posting....

you mentioned that you thought some SQL-izing would be constructive. can
you
flesh that out a little. what are you thinking about....remember this is a
newbie on this end :)



:

It's viable... just a bit flaky and difficult to control (as I think
you've
found). Your expression [Me]![Patient Number] in your dmax will only
work
if the record has already been added and that leads to timing issues
which
is where I think your problem is.

If you want a record with a specific PN and lesion number then you're
better
off writing it directly. You get way more control and debugability.


.....i forgot to propose an example of the code you had in mind
(assuming
the
idea of setting expressions on the Default properties of the Patient
and
Lesion Number fields isn't viable).

-ted

:

When you click the 'New Record' button on the standard record
navigation
bar
then, if the form is filtered, it doesn't automatically carry that
filtering
value on to the new record.

Where is the form opened from? It should (always) be another form
(or
forms) that specify the patient number. I'd tend to use code on
that
(or
those) form(s) that adds a new record via an SQL statement
(including
the PN
and the dmax+1) and then just go to that record.


i have an a2k application and a form having two controls which
make up
the
PK.
a) Patient Number
b) Lesion Number

the form is 'filtered' so when user designates a patient number to
work on
from another open form, he's taken to this one and can view only
those
PNs
selected.

there are multiple records viewable on this form (it's coninuous)
each
one
having an incrementally larger value of "Lestion Number" (from
1,2,....,12)

i wrote a few vba pieces of code shown below

Private Sub Form_Load()
LAS_EnableSecurity Me
Me.Patient_Number.Value = Forms![RECIST Disease Evaluation:
Nontarget
Lesions]![Patient Number]
End Sub

the above to handle the case when the form's loading (and was
written
for
the case when no records were entered prior)

how can i let user simultaneously duplicate a Patient Number and
increment
a
Lesion Number w/o having to do so manually.

Private Sub Patient_Number_AfterUpdate()
Me.Patient_Number.DefaultValue = "'" & [Patient Number] & "'" '
that's
a
single quote within doubles...
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1 +
DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = " &
Me![Patient
Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the upper limit",
vbInformation)
End If
DoCmd.Requery
End Sub

the above intended to duplicate the value of Patient Number and
increment
Lesion Number.

in testing this out when no other records matching the PN were
created, i
opened the form and found Patient Number filled in (by the 'load'
vba
presumably).

the 'pencil' is on the first record and the Patient Number's
completed.
the
'*' is on the record below. if i manually key in '01' into the
Lesion
Number
control it returns a run-time error '3058' abut index or pk not
being
able
to contain a null -value and highligts the DoCmd.Requery in the
vba
code
below

Private Sub Lesion_Number_AfterUpdate()
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1 +
DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = " &
Me![Patient
Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the upper limit",
vbInformation)
End If
DoCmd.Requery
End Sub


this is probably one of those 'really simple' solutions that
befuddle
the
newbie population.


but to further round things out, if i open a form with a bunch of
records
already entered and click on 'add record'. i need to manually key
in
the
PN
when i click 'add record' at which instant it automatically fills
in
the
LN,
displays a record (w/ completed PN in the '*' row) and causes the
cursor
to
jump to the topmost (first record).

????
 
R

Rob Oldfield

In the order that it appeared in the previous post. Step 1 is to build the
sql string, step 2 is to run it. This chunk of code is just the 'run it'
part.


Ted said:
okay -- it's probably worth trying it out (the default method isn't working
out for some reason anyway).

where does this code belong?

dim db as database
set db=currentdb
db.execute sql
set db=nothing

best,

-ted



Rob Oldfield said:
No. Provided that the new record needs to be written with null values for
any of the other values (or 0s for numeric fields) then just leaving them
out of the query will work fine. The actual format of how you choose to
display those fields on a form is also immaterial - bear in mind that you're
not writing data to the form - that's just a means of viewing the data -
it's going into a table.

Which of the methods you feel is simpler is always going to be down to you.
But I guarantee you 100% that the SQL method is a hugely powerful technique
that has uses in many places in Access. It *is* much better and as I
mentioned before, a great deal easier to debug.


Ted said:
in addition to the pn and ln (which comprise the PK), there are five other
controls on this same form. do they need referring to in the append query?
does it matter at all that both pn and ln are comboboxes; do they need
to
get
changed into textboxes?

i still think that the "Default" 'method' is the simpler conceptually to
understand and the one that 'feels' the 'best'.

:

I'm not saying the default value method will never work... just that it
isn't the best way. Anyway - the SQL method:

Set up a new append query (not based on any table/query) that will
add
some
values into the table your data would be adding to. Use any values
in
that
query. Then switch to the SQL view of that query (via the View
menu).
It
will look something like...

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

You now need to build that same string but instead of the random
values
1
and 2... include the actual values you want to use. That will be something
like...

dim sql as string
dim pn as long
dim ln as long
pn=.... 'whatever.. set it to the patient number you want
ln=.... 'whatever... set it to the lesion number you want
sql="INSERT INTO tblTarget ( [Patient Number], [Lesion Number] ) "+ _
"SELECT "+cstr(pn)+" AS PN, "+cstr(ln)+" AS LN;"

Doing it that way you can check that pn and ln are getting calculated
correctly, and by adding a breakpoint, check that the SQL statement
works.... by copying and pasting it into a new query window.

You can then run it by...

dim db as database
set db=currentdb
db.execute sql
set db=nothing

The last part will be just to move to that newly added record. But I
wouldn't worry about that until you get the above working.


since i don't pretend to be the final word on vba (what would i be doing
asking all this stuff), what i think you're saying is that it
won't
work
on
the Default Property of Lesion Number no matter how much i adhere
to
the
tenets of the other contributors to my posting....

you mentioned that you thought some SQL-izing would be
constructive.
can
you
flesh that out a little. what are you thinking about....remember
this
is a
newbie on this end :)



:

It's viable... just a bit flaky and difficult to control (as I think
you've
found). Your expression [Me]![Patient Number] in your dmax will only
work
if the record has already been added and that leads to timing issues
which
is where I think your problem is.

If you want a record with a specific PN and lesion number then you're
better
off writing it directly. You get way more control and debugability.


.....i forgot to propose an example of the code you had in mind
(assuming
the
idea of setting expressions on the Default properties of the Patient
and
Lesion Number fields isn't viable).

-ted

:

When you click the 'New Record' button on the standard record
navigation
bar
then, if the form is filtered, it doesn't automatically
carry
that
filtering
value on to the new record.

Where is the form opened from? It should (always) be
another
form
(or
forms) that specify the patient number. I'd tend to use code on
that
(or
those) form(s) that adds a new record via an SQL statement
(including
the PN
and the dmax+1) and then just go to that record.


i have an a2k application and a form having two controls which
make up
the
PK.
a) Patient Number
b) Lesion Number

the form is 'filtered' so when user designates a patient number to
work on
from another open form, he's taken to this one and can
view
only
those
PNs
selected.

there are multiple records viewable on this form (it's coninuous)
each
one
having an incrementally larger value of "Lestion Number" (from
1,2,....,12)

i wrote a few vba pieces of code shown below

Private Sub Form_Load()
LAS_EnableSecurity Me
Me.Patient_Number.Value = Forms![RECIST Disease Evaluation:
Nontarget
Lesions]![Patient Number]
End Sub

the above to handle the case when the form's loading (and was
written
for
the case when no records were entered prior)

how can i let user simultaneously duplicate a Patient
Number
and
increment
a
Lesion Number w/o having to do so manually.

Private Sub Patient_Number_AfterUpdate()
Me.Patient_Number.DefaultValue = "'" & [Patient Number] &
"'"
'
that's
a
single quote within doubles...
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1 +
DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = " &
Me![Patient
Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the upper limit",
vbInformation)
End If
DoCmd.Requery
End Sub

the above intended to duplicate the value of Patient
Number
and
increment
Lesion Number.

in testing this out when no other records matching the PN were
created, i
opened the form and found Patient Number filled in (by the 'load'
vba
presumably).

the 'pencil' is on the first record and the Patient Number's
completed.
the
'*' is on the record below. if i manually key in '01' into the
Lesion
Number
control it returns a run-time error '3058' abut index or
pk
not
being
able
to contain a null -value and highligts the DoCmd.Requery
in
the
vba
code
below

Private Sub Lesion_Number_AfterUpdate()
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1 +
DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = " &
Me![Patient
Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the upper limit",
vbInformation)
End If
DoCmd.Requery
End Sub


this is probably one of those 'really simple' solutions that
befuddle
the
newbie population.


but to further round things out, if i open a form with a
bunch
of
records
already entered and click on 'add record'. i need to
manually
key
in
the
PN
when i click 'add record' at which instant it
automatically
fills
in
the
LN,
displays a record (w/ completed PN in the '*' row) and
causes
the
cursor
to
jump to the topmost (first record).

????
 
G

Guest

well, i don't seem to be having all that much luck penetrating your
description of the process that this'd involve either....

here's where it's at:

i wrote the SQL

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

and saved it as "qryAppendToNonTargetLesions".

now, the trick seems to be decoding what you're saying.

is tblTarget just used as a surrogate for the real table's name, i.e.
"Lesions: Non-Target"

(btw, "Patient Number" is an eight digit integer valued number and "Lesion
Number" is a three digit integer valued number that can be no greater than
999)

when you say "You now need to build that same string but instead of the
random values 1 and 2....include the actual values you want to use" this is
where you start to lose me.





Rob Oldfield said:
In the order that it appeared in the previous post. Step 1 is to build the
sql string, step 2 is to run it. This chunk of code is just the 'run it'
part.


Ted said:
okay -- it's probably worth trying it out (the default method isn't working
out for some reason anyway).

where does this code belong?

dim db as database
set db=currentdb
db.execute sql
set db=nothing

best,

-ted



Rob Oldfield said:
No. Provided that the new record needs to be written with null values for
any of the other values (or 0s for numeric fields) then just leaving them
out of the query will work fine. The actual format of how you choose to
display those fields on a form is also immaterial - bear in mind that you're
not writing data to the form - that's just a means of viewing the data -
it's going into a table.

Which of the methods you feel is simpler is always going to be down to you.
But I guarantee you 100% that the SQL method is a hugely powerful technique
that has uses in many places in Access. It *is* much better and as I
mentioned before, a great deal easier to debug.


in addition to the pn and ln (which comprise the PK), there are five other
controls on this same form. do they need referring to in the append query?
does it matter at all that both pn and ln are comboboxes; do they need to
get
changed into textboxes?

i still think that the "Default" 'method' is the simpler conceptually to
understand and the one that 'feels' the 'best'.

:

I'm not saying the default value method will never work... just that it
isn't the best way. Anyway - the SQL method:

Set up a new append query (not based on any table/query) that will add
some
values into the table your data would be adding to. Use any values in
that
query. Then switch to the SQL view of that query (via the View menu).
It
will look something like...

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

You now need to build that same string but instead of the random values
1
and 2... include the actual values you want to use. That will be
something
like...

dim sql as string
dim pn as long
dim ln as long
pn=.... 'whatever.. set it to the patient number you want
ln=.... 'whatever... set it to the lesion number you want
sql="INSERT INTO tblTarget ( [Patient Number], [Lesion Number] ) "+ _
"SELECT "+cstr(pn)+" AS PN, "+cstr(ln)+" AS LN;"

Doing it that way you can check that pn and ln are getting calculated
correctly, and by adding a breakpoint, check that the SQL statement
works.... by copying and pasting it into a new query window.

You can then run it by...

dim db as database
set db=currentdb
db.execute sql
set db=nothing

The last part will be just to move to that newly added record. But I
wouldn't worry about that until you get the above working.


since i don't pretend to be the final word on vba (what would i be
doing
asking all this stuff), what i think you're saying is that it won't
work
on
the Default Property of Lesion Number no matter how much i adhere to
the
tenets of the other contributors to my posting....

you mentioned that you thought some SQL-izing would be constructive.
can
you
flesh that out a little. what are you thinking about....remember this
is a
newbie on this end :)



:

It's viable... just a bit flaky and difficult to control (as I think
you've
found). Your expression [Me]![Patient Number] in your dmax will
only
work
if the record has already been added and that leads to timing issues
which
is where I think your problem is.

If you want a record with a specific PN and lesion number then
you're
better
off writing it directly. You get way more control and debugability.


.....i forgot to propose an example of the code you had in mind
(assuming
the
idea of setting expressions on the Default properties of the
Patient
and
Lesion Number fields isn't viable).

-ted

:

When you click the 'New Record' button on the standard record
navigation
bar
then, if the form is filtered, it doesn't automatically carry
that
filtering
value on to the new record.

Where is the form opened from? It should (always) be another
form
(or
forms) that specify the patient number. I'd tend to use code on
that
(or
those) form(s) that adds a new record via an SQL statement
(including
the PN
and the dmax+1) and then just go to that record.


i have an a2k application and a form having two controls which
make up
the
PK.
a) Patient Number
b) Lesion Number

the form is 'filtered' so when user designates a patient
number to
work on
from another open form, he's taken to this one and can view
only
those
PNs
selected.

there are multiple records viewable on this form (it's
coninuous)
each
one
having an incrementally larger value of "Lestion Number" (from
1,2,....,12)

i wrote a few vba pieces of code shown below

Private Sub Form_Load()
LAS_EnableSecurity Me
Me.Patient_Number.Value = Forms![RECIST Disease Evaluation:
Nontarget
Lesions]![Patient Number]
End Sub

the above to handle the case when the form's loading (and was
written
for
the case when no records were entered prior)

how can i let user simultaneously duplicate a Patient Number
and
increment
a
Lesion Number w/o having to do so manually.

Private Sub Patient_Number_AfterUpdate()
Me.Patient_Number.DefaultValue = "'" & [Patient Number] & "'"
'
that's
a
single quote within doubles...
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1 +
DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = " &
Me![Patient
Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the upper
limit",
vbInformation)
End If
DoCmd.Requery
End Sub

the above intended to duplicate the value of Patient Number
and
increment
Lesion Number.

in testing this out when no other records matching the PN were
created, i
opened the form and found Patient Number filled in (by the
'load'
vba
presumably).

the 'pencil' is on the first record and the Patient Number's
completed.
the
'*' is on the record below. if i manually key in '01' into the
Lesion
Number
control it returns a run-time error '3058' abut index or pk
not
being
able
to contain a null -value and highligts the DoCmd.Requery in
the
vba
code
below

Private Sub Lesion_Number_AfterUpdate()
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1 +
DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = " &
Me![Patient
Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the upper
limit",
vbInformation)
End If
DoCmd.Requery
End Sub


this is probably one of those 'really simple' solutions that
befuddle
the
newbie population.


but to further round things out, if i open a form with a bunch
of
records
already entered and click on 'add record'. i need to manually
key
in
the
PN
when i click 'add record' at which instant it automatically
fills
in
the
LN,
displays a record (w/ completed PN in the '*' row) and causes
the
cursor
to
jump to the topmost (first record).

????
 
R

Rob Oldfield

I said that it was a better approach... not that it was easier... :)

Could I just check something... you have a button that you're clicking on a
form that is going to be triggering this routine? Does that button also
have any other code included? Anyway...

Basically, the SQL you are going to want to run is going to be of the
form...

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

....except for two things. The first one you already have - tblTarget is
indeed a surrogate for your actual table name. As your table name contains
spaces it will also need some additional square brackets... so it'd look
like...

INSERT INTO [Lesions: Non-Target] ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

The second thing is that if you run that SQL it's going to add a new record
into the table with 1 as the patient number and 2 as the lesion number...
which isn't what you want. You therefore need to build a string similar to
that one, but with the actual values you want instead of the 1 and 2. More
comments on that below.

The last point is that isn't an SQL statement that you save. It's going to
be built as and when it's needed, with the correct values included, and then
run, and then discarded.

'Set up variables.
'sql is going to be the command to be run
'pn is going to be the patient number you want to add
'ln is going to be the lesion number you want to add
dim sql as string
dim pn as long
dim ln as long

'Figure out the required values
'pn=.... 'whatever.. set it to the patient number you want
'from looking at your other posts it will be something like...
pn=[Forms]![RECIST Disease Evaluation: Nontarget Lesions]![Patient Number]

'ln=.... 'whatever... set it to the lesion number you want
'again, from looking at your other posts, something like...
ln=Nz(DMax("[Lesion Number]","[Lesions: Non-Target]","[Patient Number] = "
& [Forms]![RECIST Disease Evaluation: Nontarget Lesions]![Patient
Number]),0) + 1

You could, at this point, set a new breakpoint so that you can check that
the correct values for pn and ln are being returned, or just drop in a
msgbox pn and/or msgbox ln if you prefer. It's definitely worth doing that
because if it isn't returning the correct values then the remaining code is
certainly not going to do what you want it to.

Next, you need to create the SQL statement - but containing the values you
want
That's done just by joining bits of text together....

sql="INSERT INTO [Lesions: Non-Target] ( [Patient Number], [Lesion Number] )
"& _
"SELECT "&cstr(pn)&" AS PN, "&cstr(ln)&" AS LN;"

Again, you should check here on what the sql string looks like. It should
be exactly the same as the one with the 1 and 2 in from before, but with
your values.

Last thing... just run that sql string. That's done like this...

dim db as database
set db=currentdb
db.execute sql
set db=nothing

That, as I mentioned a while ago, will hopefully add the correct new record.
The last part, which we haven't covered yet, is to move to that newly added
record, but let's leave that for the moment. For now you will hopefully
just be able to use the navigation buttons to find it.

Good luck!


Ted said:
well, i don't seem to be having all that much luck penetrating your
description of the process that this'd involve either....

here's where it's at:

i wrote the SQL

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

and saved it as "qryAppendToNonTargetLesions".

now, the trick seems to be decoding what you're saying.

is tblTarget just used as a surrogate for the real table's name, i.e.
"Lesions: Non-Target"

(btw, "Patient Number" is an eight digit integer valued number and "Lesion
Number" is a three digit integer valued number that can be no greater than
999)

when you say "You now need to build that same string but instead of the
random values 1 and 2....include the actual values you want to use" this is
where you start to lose me.





Rob Oldfield said:
In the order that it appeared in the previous post. Step 1 is to build the
sql string, step 2 is to run it. This chunk of code is just the 'run it'
part.


Ted said:
okay -- it's probably worth trying it out (the default method isn't working
out for some reason anyway).

where does this code belong?

dim db as database
set db=currentdb
db.execute sql
set db=nothing

best,

-ted



:

No. Provided that the new record needs to be written with null
values
for
any of the other values (or 0s for numeric fields) then just leaving them
out of the query will work fine. The actual format of how you choose to
display those fields on a form is also immaterial - bear in mind
that
you're
not writing data to the form - that's just a means of viewing the data -
it's going into a table.

Which of the methods you feel is simpler is always going to be down
to
you.
But I guarantee you 100% that the SQL method is a hugely powerful technique
that has uses in many places in Access. It *is* much better and as I
mentioned before, a great deal easier to debug.


in addition to the pn and ln (which comprise the PK), there are
five
other
controls on this same form. do they need referring to in the
append
query?
does it matter at all that both pn and ln are comboboxes; do they
need
to
get
changed into textboxes?

i still think that the "Default" 'method' is the simpler
conceptually
to
understand and the one that 'feels' the 'best'.

:

I'm not saying the default value method will never work... just
that
it
isn't the best way. Anyway - the SQL method:

Set up a new append query (not based on any table/query) that
will
add
some
values into the table your data would be adding to. Use any
values
in
that
query. Then switch to the SQL view of that query (via the View menu).
It
will look something like...

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

You now need to build that same string but instead of the random values
1
and 2... include the actual values you want to use. That will be
something
like...

dim sql as string
dim pn as long
dim ln as long
pn=.... 'whatever.. set it to the patient number you want
ln=.... 'whatever... set it to the lesion number you want
sql="INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
"+
_
"SELECT "+cstr(pn)+" AS PN, "+cstr(ln)+" AS LN;"

Doing it that way you can check that pn and ln are getting calculated
correctly, and by adding a breakpoint, check that the SQL statement
works.... by copying and pasting it into a new query window.

You can then run it by...

dim db as database
set db=currentdb
db.execute sql
set db=nothing

The last part will be just to move to that newly added record.
But
I
wouldn't worry about that until you get the above working.


since i don't pretend to be the final word on vba (what would i be
doing
asking all this stuff), what i think you're saying is that it won't
work
on
the Default Property of Lesion Number no matter how much i
adhere
to
the
tenets of the other contributors to my posting....

you mentioned that you thought some SQL-izing would be constructive.
can
you
flesh that out a little. what are you thinking
about....remember
this
is a
newbie on this end :)



:

It's viable... just a bit flaky and difficult to control (as
I
think
you've
found). Your expression [Me]![Patient Number] in your dmax will
only
work
if the record has already been added and that leads to
timing
issues
which
is where I think your problem is.

If you want a record with a specific PN and lesion number then
you're
better
off writing it directly. You get way more control and debugability.


.....i forgot to propose an example of the code you had in mind
(assuming
the
idea of setting expressions on the Default properties of the
Patient
and
Lesion Number fields isn't viable).

-ted

:

When you click the 'New Record' button on the standard record
navigation
bar
then, if the form is filtered, it doesn't automatically carry
that
filtering
value on to the new record.

Where is the form opened from? It should (always) be another
form
(or
forms) that specify the patient number. I'd tend to use code on
that
(or
those) form(s) that adds a new record via an SQL statement
(including
the PN
and the dmax+1) and then just go to that record.


i have an a2k application and a form having two
controls
which
make up
the
PK.
a) Patient Number
b) Lesion Number

the form is 'filtered' so when user designates a patient
number to
work on
from another open form, he's taken to this one and can view
only
those
PNs
selected.

there are multiple records viewable on this form (it's
coninuous)
each
one
having an incrementally larger value of "Lestion
Number"
(from
1,2,....,12)

i wrote a few vba pieces of code shown below

Private Sub Form_Load()
LAS_EnableSecurity Me
Me.Patient_Number.Value = Forms![RECIST Disease Evaluation:
Nontarget
Lesions]![Patient Number]
End Sub

the above to handle the case when the form's loading
(and
was
written
for
the case when no records were entered prior)

how can i let user simultaneously duplicate a Patient Number
and
increment
a
Lesion Number w/o having to do so manually.

Private Sub Patient_Number_AfterUpdate()
Me.Patient_Number.DefaultValue = "'" & [Patient
Number] &
"'"
'
that's
a
single quote within doubles...
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1 +
DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = " &
Me![Patient
Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the upper
limit",
vbInformation)
End If
DoCmd.Requery
End Sub

the above intended to duplicate the value of Patient Number
and
increment
Lesion Number.

in testing this out when no other records matching the
PN
were
created, i
opened the form and found Patient Number filled in (by the
'load'
vba
presumably).

the 'pencil' is on the first record and the Patient Number's
completed.
the
'*' is on the record below. if i manually key in '01'
into
the
Lesion
Number
control it returns a run-time error '3058' abut index
or
pk
not
being
able
to contain a null -value and highligts the
DoCmd.Requery
in
the
vba
code
below

Private Sub Lesion_Number_AfterUpdate()
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1 +
DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = " &
Me![Patient
Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the upper
limit",
vbInformation)
End If
DoCmd.Requery
End Sub


this is probably one of those 'really simple'
solutions
that
befuddle
the
newbie population.


but to further round things out, if i open a form with
a
bunch
of
records
already entered and click on 'add record'. i need to manually
key
in
the
PN
when i click 'add record' at which instant it automatically
fills
in
the
LN,
displays a record (w/ completed PN in the '*' row) and causes
the
cursor
to
jump to the topmost (first record).

????
 
G

Guest

well let's start off by seeing how much 'better' it's going to be, however
better is defined.

regarding the existence of a button. it doesn't, however if it'll get the
showboat moving i could create one -- which answers your question wrt what
other code there IS on it -- NONE.

since i'm writing this from home and w/o the aid and comfort of the database
itself, i'll have to begin it no sooner than manyana morning. but i can see
that even now i'm unclear as to where you would have this concoction of ours
belong. i mean you asked if i had a button established .... so as to wrap it
up in an event trigger/property?

-ted


Rob Oldfield said:
I said that it was a better approach... not that it was easier... :)

Could I just check something... you have a button that you're clicking on a
form that is going to be triggering this routine? Does that button also
have any other code included? Anyway...

Basically, the SQL you are going to want to run is going to be of the
form...

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

....except for two things. The first one you already have - tblTarget is
indeed a surrogate for your actual table name. As your table name contains
spaces it will also need some additional square brackets... so it'd look
like...

INSERT INTO [Lesions: Non-Target] ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

The second thing is that if you run that SQL it's going to add a new record
into the table with 1 as the patient number and 2 as the lesion number...
which isn't what you want. You therefore need to build a string similar to
that one, but with the actual values you want instead of the 1 and 2. More
comments on that below.

The last point is that isn't an SQL statement that you save. It's going to
be built as and when it's needed, with the correct values included, and then
run, and then discarded.

'Set up variables.
'sql is going to be the command to be run
'pn is going to be the patient number you want to add
'ln is going to be the lesion number you want to add
dim sql as string
dim pn as long
dim ln as long

'Figure out the required values
'pn=.... 'whatever.. set it to the patient number you want
'from looking at your other posts it will be something like...
pn=[Forms]![RECIST Disease Evaluation: Nontarget Lesions]![Patient Number]

'ln=.... 'whatever... set it to the lesion number you want
'again, from looking at your other posts, something like...
ln=Nz(DMax("[Lesion Number]","[Lesions: Non-Target]","[Patient Number] = "
& [Forms]![RECIST Disease Evaluation: Nontarget Lesions]![Patient
Number]),0) + 1

You could, at this point, set a new breakpoint so that you can check that
the correct values for pn and ln are being returned, or just drop in a
msgbox pn and/or msgbox ln if you prefer. It's definitely worth doing that
because if it isn't returning the correct values then the remaining code is
certainly not going to do what you want it to.

Next, you need to create the SQL statement - but containing the values you
want
That's done just by joining bits of text together....

sql="INSERT INTO [Lesions: Non-Target] ( [Patient Number], [Lesion Number] )
"& _
"SELECT "&cstr(pn)&" AS PN, "&cstr(ln)&" AS LN;"

Again, you should check here on what the sql string looks like. It should
be exactly the same as the one with the 1 and 2 in from before, but with
your values.

Last thing... just run that sql string. That's done like this...

dim db as database
set db=currentdb
db.execute sql
set db=nothing

That, as I mentioned a while ago, will hopefully add the correct new record.
The last part, which we haven't covered yet, is to move to that newly added
record, but let's leave that for the moment. For now you will hopefully
just be able to use the navigation buttons to find it.

Good luck!


Ted said:
well, i don't seem to be having all that much luck penetrating your
description of the process that this'd involve either....

here's where it's at:

i wrote the SQL

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

and saved it as "qryAppendToNonTargetLesions".

now, the trick seems to be decoding what you're saying.

is tblTarget just used as a surrogate for the real table's name, i.e.
"Lesions: Non-Target"

(btw, "Patient Number" is an eight digit integer valued number and "Lesion
Number" is a three digit integer valued number that can be no greater than
999)

when you say "You now need to build that same string but instead of the
random values 1 and 2....include the actual values you want to use" this is
where you start to lose me.





Rob Oldfield said:
In the order that it appeared in the previous post. Step 1 is to build the
sql string, step 2 is to run it. This chunk of code is just the 'run it'
part.


okay -- it's probably worth trying it out (the default method isn't
working
out for some reason anyway).

where does this code belong?

dim db as database
set db=currentdb
db.execute sql
set db=nothing

best,

-ted



:

No. Provided that the new record needs to be written with null values
for
any of the other values (or 0s for numeric fields) then just leaving
them
out of the query will work fine. The actual format of how you choose to
display those fields on a form is also immaterial - bear in mind that
you're
not writing data to the form - that's just a means of viewing the data -
it's going into a table.

Which of the methods you feel is simpler is always going to be down to
you.
But I guarantee you 100% that the SQL method is a hugely powerful
technique
that has uses in many places in Access. It *is* much better and as I
mentioned before, a great deal easier to debug.


in addition to the pn and ln (which comprise the PK), there are five
other
controls on this same form. do they need referring to in the append
query?
does it matter at all that both pn and ln are comboboxes; do they need
to
get
changed into textboxes?

i still think that the "Default" 'method' is the simpler conceptually
to
understand and the one that 'feels' the 'best'.

:

I'm not saying the default value method will never work... just that
it
isn't the best way. Anyway - the SQL method:

Set up a new append query (not based on any table/query) that will
add
some
values into the table your data would be adding to. Use any values
in
that
query. Then switch to the SQL view of that query (via the View
menu).
It
will look something like...

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

You now need to build that same string but instead of the random
values
1
and 2... include the actual values you want to use. That will be
something
like...

dim sql as string
dim pn as long
dim ln as long
pn=.... 'whatever.. set it to the patient number you want
ln=.... 'whatever... set it to the lesion number you want
sql="INSERT INTO tblTarget ( [Patient Number], [Lesion Number] ) "+
_
"SELECT "+cstr(pn)+" AS PN, "+cstr(ln)+" AS LN;"

Doing it that way you can check that pn and ln are getting
calculated
correctly, and by adding a breakpoint, check that the SQL statement
works.... by copying and pasting it into a new query window.

You can then run it by...

dim db as database
set db=currentdb
db.execute sql
set db=nothing

The last part will be just to move to that newly added record. But
I
wouldn't worry about that until you get the above working.


since i don't pretend to be the final word on vba (what would i be
doing
asking all this stuff), what i think you're saying is that it
won't
work
on
the Default Property of Lesion Number no matter how much i adhere
to
the
tenets of the other contributors to my posting....

you mentioned that you thought some SQL-izing would be
constructive.
can
you
flesh that out a little. what are you thinking about....remember
this
is a
newbie on this end :)



:

It's viable... just a bit flaky and difficult to control (as I
think
you've
found). Your expression [Me]![Patient Number] in your dmax will
only
work
if the record has already been added and that leads to timing
issues
which
is where I think your problem is.

If you want a record with a specific PN and lesion number then
you're
better
off writing it directly. You get way more control and
debugability.


.....i forgot to propose an example of the code you had in
mind
(assuming
the
idea of setting expressions on the Default properties of the
Patient
and
Lesion Number fields isn't viable).

-ted

:

When you click the 'New Record' button on the standard
record
navigation
bar
then, if the form is filtered, it doesn't automatically
carry
that
filtering
value on to the new record.

Where is the form opened from? It should (always) be
another
form
(or
forms) that specify the patient number. I'd tend to use
code on
that
(or
those) form(s) that adds a new record via an SQL statement
(including
the PN
and the dmax+1) and then just go to that record.


i have an a2k application and a form having two controls
which
make up
the
PK.
a) Patient Number
b) Lesion Number

the form is 'filtered' so when user designates a patient
number to
work on
from another open form, he's taken to this one and can
view
only
those
PNs
selected.

there are multiple records viewable on this form (it's
coninuous)
each
one
having an incrementally larger value of "Lestion Number"
(from
1,2,....,12)

i wrote a few vba pieces of code shown below

Private Sub Form_Load()
LAS_EnableSecurity Me
Me.Patient_Number.Value = Forms![RECIST Disease
Evaluation:
Nontarget
Lesions]![Patient Number]
End Sub

the above to handle the case when the form's loading (and
was
written
for
the case when no records were entered prior)

how can i let user simultaneously duplicate a Patient
Number
and
increment
a
Lesion Number w/o having to do so manually.

Private Sub Patient_Number_AfterUpdate()
Me.Patient_Number.DefaultValue = "'" & [Patient Number] &
"'"
'
that's
a
single quote within doubles...
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1 +
DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = " &
Me![Patient
Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the upper
limit",
vbInformation)
End If
DoCmd.Requery
End Sub

the above intended to duplicate the value of Patient
Number
and
increment
Lesion Number.

in testing this out when no other records matching the PN
were
created, i
opened the form and found Patient Number filled in (by the
'load'
vba
presumably).

the 'pencil' is on the first record and the Patient
Number's
completed.
the
'*' is on the record below. if i manually key in '01' into
the
Lesion
Number
control it returns a run-time error '3058' abut index or
pk
not
being
able
to contain a null -value and highligts the DoCmd.Requery
in
the
vba
code
below

Private Sub Lesion_Number_AfterUpdate()
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1 +
DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = " &
Me![Patient
Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the upper
limit",
vbInformation)
End If
DoCmd.Requery
End Sub


this is probably one of those 'really simple' solutions
that
befuddle
the
newbie population.


but to further round things out, if i open a form with a
bunch
of
records
already entered and click on 'add record'. i need to
manually
key
in
the
PN
when i click 'add record' at which instant it
automatically
fills
in
the
LN,
displays a record (w/ completed PN in the '*' row) and
causes
the
cursor
to
jump to the topmost (first record).

????
 
R

Rob Oldfield

Exactly. I'd see it as a 'New' button which would add the record and take
you to it.

(...and what were you using to trigger the code before?)


Ted said:
well let's start off by seeing how much 'better' it's going to be, however
better is defined.

regarding the existence of a button. it doesn't, however if it'll get the
showboat moving i could create one -- which answers your question wrt what
other code there IS on it -- NONE.

since i'm writing this from home and w/o the aid and comfort of the database
itself, i'll have to begin it no sooner than manyana morning. but i can see
that even now i'm unclear as to where you would have this concoction of ours
belong. i mean you asked if i had a button established .... so as to wrap it
up in an event trigger/property?

-ted


Rob Oldfield said:
I said that it was a better approach... not that it was easier... :)

Could I just check something... you have a button that you're clicking on a
form that is going to be triggering this routine? Does that button also
have any other code included? Anyway...

Basically, the SQL you are going to want to run is going to be of the
form...

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

....except for two things. The first one you already have - tblTarget is
indeed a surrogate for your actual table name. As your table name contains
spaces it will also need some additional square brackets... so it'd look
like...

INSERT INTO [Lesions: Non-Target] ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

The second thing is that if you run that SQL it's going to add a new record
into the table with 1 as the patient number and 2 as the lesion number...
which isn't what you want. You therefore need to build a string similar to
that one, but with the actual values you want instead of the 1 and 2. More
comments on that below.

The last point is that isn't an SQL statement that you save. It's going to
be built as and when it's needed, with the correct values included, and then
run, and then discarded.

'Set up variables.
'sql is going to be the command to be run
'pn is going to be the patient number you want to add
'ln is going to be the lesion number you want to add
dim sql as string
dim pn as long
dim ln as long

'Figure out the required values
'pn=.... 'whatever.. set it to the patient number you want
'from looking at your other posts it will be something like...
pn=[Forms]![RECIST Disease Evaluation: Nontarget Lesions]![Patient Number]

'ln=.... 'whatever... set it to the lesion number you want
'again, from looking at your other posts, something like...
ln=Nz(DMax("[Lesion Number]","[Lesions: Non-Target]","[Patient Number] = "
& [Forms]![RECIST Disease Evaluation: Nontarget Lesions]![Patient
Number]),0) + 1

You could, at this point, set a new breakpoint so that you can check that
the correct values for pn and ln are being returned, or just drop in a
msgbox pn and/or msgbox ln if you prefer. It's definitely worth doing that
because if it isn't returning the correct values then the remaining code is
certainly not going to do what you want it to.

Next, you need to create the SQL statement - but containing the values you
want
That's done just by joining bits of text together....

sql="INSERT INTO [Lesions: Non-Target] ( [Patient Number], [Lesion Number] )
"& _
"SELECT "&cstr(pn)&" AS PN, "&cstr(ln)&" AS LN;"

Again, you should check here on what the sql string looks like. It should
be exactly the same as the one with the 1 and 2 in from before, but with
your values.

Last thing... just run that sql string. That's done like this...

dim db as database
set db=currentdb
db.execute sql
set db=nothing

That, as I mentioned a while ago, will hopefully add the correct new record.
The last part, which we haven't covered yet, is to move to that newly added
record, but let's leave that for the moment. For now you will hopefully
just be able to use the navigation buttons to find it.

Good luck!


Ted said:
well, i don't seem to be having all that much luck penetrating your
description of the process that this'd involve either....

here's where it's at:

i wrote the SQL

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

and saved it as "qryAppendToNonTargetLesions".

now, the trick seems to be decoding what you're saying.

is tblTarget just used as a surrogate for the real table's name, i.e.
"Lesions: Non-Target"

(btw, "Patient Number" is an eight digit integer valued number and "Lesion
Number" is a three digit integer valued number that can be no greater than
999)

when you say "You now need to build that same string but instead of the
random values 1 and 2....include the actual values you want to use"
this
is
where you start to lose me.





:

In the order that it appeared in the previous post. Step 1 is to
build
the
sql string, step 2 is to run it. This chunk of code is just the
'run
it'
part.


okay -- it's probably worth trying it out (the default method isn' t
working
out for some reason anyway).

where does this code belong?

dim db as database
set db=currentdb
db.execute sql
set db=nothing

best,

-ted



:

No. Provided that the new record needs to be written with null values
for
any of the other values (or 0s for numeric fields) then just leaving
them
out of the query will work fine. The actual format of how you choose to
display those fields on a form is also immaterial - bear in mind that
you're
not writing data to the form - that's just a means of viewing
the
data -
it's going into a table.

Which of the methods you feel is simpler is always going to be
down
to
you.
But I guarantee you 100% that the SQL method is a hugely powerful
technique
that has uses in many places in Access. It *is* much better and
as
I
mentioned before, a great deal easier to debug.


in addition to the pn and ln (which comprise the PK), there
are
five
other
controls on this same form. do they need referring to in the append
query?
does it matter at all that both pn and ln are comboboxes; do
they
need
to
get
changed into textboxes?

i still think that the "Default" 'method' is the simpler conceptually
to
understand and the one that 'feels' the 'best'.

:

I'm not saying the default value method will never work...
just
that
it
isn't the best way. Anyway - the SQL method:

Set up a new append query (not based on any table/query)
that
will
add
some
values into the table your data would be adding to. Use any values
in
that
query. Then switch to the SQL view of that query (via the View
menu).
It
will look something like...

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

You now need to build that same string but instead of the random
values
1
and 2... include the actual values you want to use. That
will
be
something
like...

dim sql as string
dim pn as long
dim ln as long
pn=.... 'whatever.. set it to the patient number you want
ln=.... 'whatever... set it to the lesion number you want
sql="INSERT INTO tblTarget ( [Patient Number], [Lesion
Number] )
"+
_
"SELECT "+cstr(pn)+" AS PN, "+cstr(ln)+" AS LN;"

Doing it that way you can check that pn and ln are getting
calculated
correctly, and by adding a breakpoint, check that the SQL statement
works.... by copying and pasting it into a new query window.

You can then run it by...

dim db as database
set db=currentdb
db.execute sql
set db=nothing

The last part will be just to move to that newly added
record.
But
I
wouldn't worry about that until you get the above working.


since i don't pretend to be the final word on vba (what
would
i be
doing
asking all this stuff), what i think you're saying is that it
won't
work
on
the Default Property of Lesion Number no matter how much i adhere
to
the
tenets of the other contributors to my posting....

you mentioned that you thought some SQL-izing would be
constructive.
can
you
flesh that out a little. what are you thinking about....remember
this
is a
newbie on this end :)



:

It's viable... just a bit flaky and difficult to control
(as
I
think
you've
found). Your expression [Me]![Patient Number] in your
dmax
will
only
work
if the record has already been added and that leads to timing
issues
which
is where I think your problem is.

If you want a record with a specific PN and lesion
number
then
you're
better
off writing it directly. You get way more control and
debugability.


.....i forgot to propose an example of the code you had in
mind
(assuming
the
idea of setting expressions on the Default properties
of
the
Patient
and
Lesion Number fields isn't viable).

-ted

:

When you click the 'New Record' button on the standard
record
navigation
bar
then, if the form is filtered, it doesn't automatically
carry
that
filtering
value on to the new record.

Where is the form opened from? It should (always) be
another
form
(or
forms) that specify the patient number. I'd tend to use
code on
that
(or
those) form(s) that adds a new record via an SQL statement
(including
the PN
and the dmax+1) and then just go to that record.


i have an a2k application and a form having two controls
which
make up
the
PK.
a) Patient Number
b) Lesion Number

the form is 'filtered' so when user designates a patient
number to
work on
from another open form, he's taken to this one and can
view
only
those
PNs
selected.

there are multiple records viewable on this form (it's
coninuous)
each
one
having an incrementally larger value of "Lestion Number"
(from
1,2,....,12)

i wrote a few vba pieces of code shown below

Private Sub Form_Load()
LAS_EnableSecurity Me
Me.Patient_Number.Value = Forms![RECIST Disease
Evaluation:
Nontarget
Lesions]![Patient Number]
End Sub

the above to handle the case when the form's
loading
(and
was
written
for
the case when no records were entered prior)

how can i let user simultaneously duplicate a Patient
Number
and
increment
a
Lesion Number w/o having to do so manually.

Private Sub Patient_Number_AfterUpdate()
Me.Patient_Number.DefaultValue = "'" & [Patient Number] &
"'"
'
that's
a
single quote within doubles...
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"),
1, 1
+
DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number]
= "
&
Me![Patient
Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding
the
upper
limit",
vbInformation)
End If
DoCmd.Requery
End Sub

the above intended to duplicate the value of Patient
Number
and
increment
Lesion Number.

in testing this out when no other records matching
the
PN
were
created, i
opened the form and found Patient Number filled in
(by
the
'load'
vba
presumably).

the 'pencil' is on the first record and the Patient
Number's
completed.
the
'*' is on the record below. if i manually key in
'01'
into
the
Lesion
Number
control it returns a run-time error '3058' abut
index
or
pk
not
being
able
to contain a null -value and highligts the DoCmd.Requery
in
the
vba
code
below

Private Sub Lesion_Number_AfterUpdate()
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"),
1, 1
+
DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number]
= "
&
Me![Patient
Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding
the
upper
limit",
vbInformation)
End If
DoCmd.Requery
End Sub


this is probably one of those 'really simple' solutions
that
befuddle
the
newbie population.


but to further round things out, if i open a form
with
a
bunch
of
records
already entered and click on 'add record'. i need to
manually
key
in
the
PN
when i click 'add record' at which instant it
automatically
fills
in
the
LN,
displays a record (w/ completed PN in the '*' row) and
causes
the
cursor
to
jump to the topmost (first record).

????
 
G

Guest

okay, here's where things stand at this point.

i created an 'Add Record' button on my form. i commented out the docmd stuff
and added what i took your (difficult to follow) instructions to boil down to
(per below)

Private Sub Add_Record_Click()
On Error GoTo Err_Add_Record_Click
Dim sql As String
Dim pn As Long
Dim ln As Integer

pn = [Forms]![RECIST Disease Evaluation: Nontarget Lesions]![Patient
Number]
ln = Nz(DMax("[Lesion Number]", "[Lesions: Non-Target]", "[Patient
Number] = " _
& [Forms]![RECIST Disease Evaluation: Nontarget Lesions]![Patient
Number]), 0) + 1

sql = "INSERT INTO [Lesions: Non-Target] ( [Patient Number], [Lesion
Number] )" & _
"SELECT " & CStr(pn) & " AS PN, " & CStr(ln) & " AS LN;"

Dim db As Database
Set db = CurrentDb
db.Execute sql
Set db = Nothing

' DoCmd.GoToRecord , , acNewRec

Exit_Add_Record_Click:
Exit Sub

Err_Add_Record_Click:
MsgBox Err.description
Resume Exit_Add_Record_Click

End Sub

i assumed that you were trying to do as you read your email from top to
bottom is motivate an essential concept/building block and then stack upon it
yet more and more information leaving the reader with the feeling, hey
presto! i have got the code down!

if that's the case then something's gone awry because it didn't work as
hoped for :-(

ah yes, to answer your earlier question, i was using the add record asterisk
thing (*) at the bottom of the form to add records. i now would disable the
'add record' property on this form (which i have and it still didn't work).

so, where's the achilees' heel in all of this?

-ted


Rob Oldfield said:
Exactly. I'd see it as a 'New' button which would add the record and take
you to it.

(...and what were you using to trigger the code before?)


Ted said:
well let's start off by seeing how much 'better' it's going to be, however
better is defined.

regarding the existence of a button. it doesn't, however if it'll get the
showboat moving i could create one -- which answers your question wrt what
other code there IS on it -- NONE.

since i'm writing this from home and w/o the aid and comfort of the database
itself, i'll have to begin it no sooner than manyana morning. but i can see
that even now i'm unclear as to where you would have this concoction of ours
belong. i mean you asked if i had a button established .... so as to wrap it
up in an event trigger/property?

-ted


Rob Oldfield said:
I said that it was a better approach... not that it was easier... :)

Could I just check something... you have a button that you're clicking on a
form that is going to be triggering this routine? Does that button also
have any other code included? Anyway...

Basically, the SQL you are going to want to run is going to be of the
form...

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

....except for two things. The first one you already have - tblTarget is
indeed a surrogate for your actual table name. As your table name contains
spaces it will also need some additional square brackets... so it'd look
like...

INSERT INTO [Lesions: Non-Target] ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

The second thing is that if you run that SQL it's going to add a new record
into the table with 1 as the patient number and 2 as the lesion number...
which isn't what you want. You therefore need to build a string similar to
that one, but with the actual values you want instead of the 1 and 2. More
comments on that below.

The last point is that isn't an SQL statement that you save. It's going to
be built as and when it's needed, with the correct values included, and then
run, and then discarded.

'Set up variables.
'sql is going to be the command to be run
'pn is going to be the patient number you want to add
'ln is going to be the lesion number you want to add
dim sql as string
dim pn as long
dim ln as long

'Figure out the required values
'pn=.... 'whatever.. set it to the patient number you want
'from looking at your other posts it will be something like...
pn=[Forms]![RECIST Disease Evaluation: Nontarget Lesions]![Patient Number]

'ln=.... 'whatever... set it to the lesion number you want
'again, from looking at your other posts, something like...
ln=Nz(DMax("[Lesion Number]","[Lesions: Non-Target]","[Patient Number] = "
& [Forms]![RECIST Disease Evaluation: Nontarget Lesions]![Patient
Number]),0) + 1

You could, at this point, set a new breakpoint so that you can check that
the correct values for pn and ln are being returned, or just drop in a
msgbox pn and/or msgbox ln if you prefer. It's definitely worth doing that
because if it isn't returning the correct values then the remaining code is
certainly not going to do what you want it to.

Next, you need to create the SQL statement - but containing the values you
want
That's done just by joining bits of text together....

sql="INSERT INTO [Lesions: Non-Target] ( [Patient Number], [Lesion Number] )
"& _
"SELECT "&cstr(pn)&" AS PN, "&cstr(ln)&" AS LN;"

Again, you should check here on what the sql string looks like. It should
be exactly the same as the one with the 1 and 2 in from before, but with
your values.

Last thing... just run that sql string. That's done like this...

dim db as database
set db=currentdb
db.execute sql
set db=nothing

That, as I mentioned a while ago, will hopefully add the correct new record.
The last part, which we haven't covered yet, is to move to that newly added
record, but let's leave that for the moment. For now you will hopefully
just be able to use the navigation buttons to find it.

Good luck!


well, i don't seem to be having all that much luck penetrating your
description of the process that this'd involve either....

here's where it's at:

i wrote the SQL

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

and saved it as "qryAppendToNonTargetLesions".

now, the trick seems to be decoding what you're saying.

is tblTarget just used as a surrogate for the real table's name, i.e.
"Lesions: Non-Target"

(btw, "Patient Number" is an eight digit integer valued number and "Lesion
Number" is a three digit integer valued number that can be no greater than
999)

when you say "You now need to build that same string but instead of the
random values 1 and 2....include the actual values you want to use" this
is
where you start to lose me.





:

In the order that it appeared in the previous post. Step 1 is to build
the
sql string, step 2 is to run it. This chunk of code is just the 'run
it'
part.


okay -- it's probably worth trying it out (the default method isn' t
working
out for some reason anyway).

where does this code belong?

dim db as database
set db=currentdb
db.execute sql
set db=nothing

best,

-ted



:

No. Provided that the new record needs to be written with null
values
for
any of the other values (or 0s for numeric fields) then just leaving
them
out of the query will work fine. The actual format of how you
choose to
display those fields on a form is also immaterial - bear in mind
that
you're
not writing data to the form - that's just a means of viewing the
data -
it's going into a table.

Which of the methods you feel is simpler is always going to be down
to
you.
But I guarantee you 100% that the SQL method is a hugely powerful
technique
that has uses in many places in Access. It *is* much better and as
I
mentioned before, a great deal easier to debug.


in addition to the pn and ln (which comprise the PK), there are
five
other
controls on this same form. do they need referring to in the
append
query?
does it matter at all that both pn and ln are comboboxes; do they
need
to
get
changed into textboxes?

i still think that the "Default" 'method' is the simpler
conceptually
to
understand and the one that 'feels' the 'best'.

:

I'm not saying the default value method will never work... just
that
it
isn't the best way. Anyway - the SQL method:

Set up a new append query (not based on any table/query) that
will
add
some
values into the table your data would be adding to. Use any
values
in
that
query. Then switch to the SQL view of that query (via the View
menu).
It
will look something like...

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

You now need to build that same string but instead of the random
values
1
and 2... include the actual values you want to use. That will
be
something
like...

dim sql as string
dim pn as long
dim ln as long
pn=.... 'whatever.. set it to the patient number you want
ln=.... 'whatever... set it to the lesion number you want
sql="INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
"+
_
"SELECT "+cstr(pn)+" AS PN, "+cstr(ln)+" AS LN;"

Doing it that way you can check that pn and ln are getting
calculated
correctly, and by adding a breakpoint, check that the SQL
statement
works.... by copying and pasting it into a new query window.

You can then run it by...

dim db as database
set db=currentdb
db.execute sql
set db=nothing

The last part will be just to move to that newly added record.
But
I
wouldn't worry about that until you get the above working.


since i don't pretend to be the final word on vba (what would
i be
doing
asking all this stuff), what i think you're saying is that it
won't
work
on
the Default Property of Lesion Number no matter how much i
adhere
to
the
tenets of the other contributors to my posting....

you mentioned that you thought some SQL-izing would be
constructive.
can
you
flesh that out a little. what are you thinking
about....remember
this
is a
newbie on this end :)



:

It's viable... just a bit flaky and difficult to control (as
I
think
you've
found). Your expression [Me]![Patient Number] in your dmax
will
only
work
if the record has already been added and that leads to
timing
issues
which
is where I think your problem is.

If you want a record with a specific PN and lesion number
then
you're
better
off writing it directly. You get way more control and
debugability.


.....i forgot to propose an example of the code you had in
mind
(assuming
the
idea of setting expressions on the Default properties of
the
Patient
and
Lesion Number fields isn't viable).

-ted

:

When you click the 'New Record' button on the standard
record
navigation
bar
then, if the form is filtered, it doesn't automatically
carry
that
filtering
value on to the new record.

Where is the form opened from? It should (always) be
another
form
(or
forms) that specify the patient number. I'd tend to use
code on
that
(or
those) form(s) that adds a new record via an SQL
statement
(including
the PN
and the dmax+1) and then just go to that record.



i have an a2k application and a form having two
controls
which
make up
the
PK.
a) Patient Number
b) Lesion Number

the form is 'filtered' so when user designates a
patient
number to
work on
from another open form, he's taken to this one and can
view
only
those
PNs
selected.

there are multiple records viewable on this form (it's
coninuous)
each
one
having an incrementally larger value of "Lestion
Number"
(from
1,2,....,12)

i wrote a few vba pieces of code shown below

Private Sub Form_Load()
LAS_EnableSecurity Me
Me.Patient_Number.Value = Forms![RECIST Disease
Evaluation:
Nontarget
Lesions]![Patient Number]
End Sub

the above to handle the case when the form's loading
(and
was
written
for
the case when no records were entered prior)

how can i let user simultaneously duplicate a Patient
Number
and
increment
a
Lesion Number w/o having to do so manually.

Private Sub Patient_Number_AfterUpdate()
Me.Patient_Number.DefaultValue = "'" & [Patient
Number] &
"'"
'
that's
a
single quote within doubles...
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1
+
DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = "
&
Me![Patient
Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the
upper
limit",
vbInformation)
End If
DoCmd.Requery
End Sub

the above intended to duplicate the value of Patient
Number
and
increment
Lesion Number.

in testing this out when no other records matching the
PN
were
created, i
opened the form and found Patient Number filled in (by
the
'load'
vba
presumably).

the 'pencil' is on the first record and the Patient
Number's
completed.
the
'*' is on the record below. if i manually key in '01'
into
the
Lesion
Number
control it returns a run-time error '3058' abut index
or
pk
not
being
able
to contain a null -value and highligts the
DoCmd.Requery
in
the
vba
code
below

Private Sub Lesion_Number_AfterUpdate()
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1
+
DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = "
&
Me![Patient
Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the
upper
limit",
vbInformation)
End If
DoCmd.Requery
End Sub


this is probably one of those 'really simple'
solutions
that
befuddle
the
newbie population.


but to further round things out, if i open a form with
a
bunch
of
records
already entered and click on 'add record'. i need to
manually
key
in
the
PN
when i click 'add record' at which instant it
automatically
fills
in
the
LN,
displays a record (w/ completed PN in the '*' row) and
causes
the
cursor
to
jump to the topmost (first record).

????
 
G

Guest

woops....i think i spoke in haste; i looked at the table underlying the form
and found, 'lo and behold, the records (blank in every respect but for the
values of Patient and Lesion numbers) that clicking the button i told you i'd
added must've generated :)

so it looks like we're converging!

the next thing you're going to undoubtedly tackle is displaying these
'hidden' records on the form.

-ted


Rob Oldfield said:
Exactly. I'd see it as a 'New' button which would add the record and take
you to it.

(...and what were you using to trigger the code before?)


Ted said:
well let's start off by seeing how much 'better' it's going to be, however
better is defined.

regarding the existence of a button. it doesn't, however if it'll get the
showboat moving i could create one -- which answers your question wrt what
other code there IS on it -- NONE.

since i'm writing this from home and w/o the aid and comfort of the database
itself, i'll have to begin it no sooner than manyana morning. but i can see
that even now i'm unclear as to where you would have this concoction of ours
belong. i mean you asked if i had a button established .... so as to wrap it
up in an event trigger/property?

-ted


Rob Oldfield said:
I said that it was a better approach... not that it was easier... :)

Could I just check something... you have a button that you're clicking on a
form that is going to be triggering this routine? Does that button also
have any other code included? Anyway...

Basically, the SQL you are going to want to run is going to be of the
form...

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

....except for two things. The first one you already have - tblTarget is
indeed a surrogate for your actual table name. As your table name contains
spaces it will also need some additional square brackets... so it'd look
like...

INSERT INTO [Lesions: Non-Target] ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

The second thing is that if you run that SQL it's going to add a new record
into the table with 1 as the patient number and 2 as the lesion number...
which isn't what you want. You therefore need to build a string similar to
that one, but with the actual values you want instead of the 1 and 2. More
comments on that below.

The last point is that isn't an SQL statement that you save. It's going to
be built as and when it's needed, with the correct values included, and then
run, and then discarded.

'Set up variables.
'sql is going to be the command to be run
'pn is going to be the patient number you want to add
'ln is going to be the lesion number you want to add
dim sql as string
dim pn as long
dim ln as long

'Figure out the required values
'pn=.... 'whatever.. set it to the patient number you want
'from looking at your other posts it will be something like...
pn=[Forms]![RECIST Disease Evaluation: Nontarget Lesions]![Patient Number]

'ln=.... 'whatever... set it to the lesion number you want
'again, from looking at your other posts, something like...
ln=Nz(DMax("[Lesion Number]","[Lesions: Non-Target]","[Patient Number] = "
& [Forms]![RECIST Disease Evaluation: Nontarget Lesions]![Patient
Number]),0) + 1

You could, at this point, set a new breakpoint so that you can check that
the correct values for pn and ln are being returned, or just drop in a
msgbox pn and/or msgbox ln if you prefer. It's definitely worth doing that
because if it isn't returning the correct values then the remaining code is
certainly not going to do what you want it to.

Next, you need to create the SQL statement - but containing the values you
want
That's done just by joining bits of text together....

sql="INSERT INTO [Lesions: Non-Target] ( [Patient Number], [Lesion Number] )
"& _
"SELECT "&cstr(pn)&" AS PN, "&cstr(ln)&" AS LN;"

Again, you should check here on what the sql string looks like. It should
be exactly the same as the one with the 1 and 2 in from before, but with
your values.

Last thing... just run that sql string. That's done like this...

dim db as database
set db=currentdb
db.execute sql
set db=nothing

That, as I mentioned a while ago, will hopefully add the correct new record.
The last part, which we haven't covered yet, is to move to that newly added
record, but let's leave that for the moment. For now you will hopefully
just be able to use the navigation buttons to find it.

Good luck!


well, i don't seem to be having all that much luck penetrating your
description of the process that this'd involve either....

here's where it's at:

i wrote the SQL

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

and saved it as "qryAppendToNonTargetLesions".

now, the trick seems to be decoding what you're saying.

is tblTarget just used as a surrogate for the real table's name, i.e.
"Lesions: Non-Target"

(btw, "Patient Number" is an eight digit integer valued number and "Lesion
Number" is a three digit integer valued number that can be no greater than
999)

when you say "You now need to build that same string but instead of the
random values 1 and 2....include the actual values you want to use" this
is
where you start to lose me.





:

In the order that it appeared in the previous post. Step 1 is to build
the
sql string, step 2 is to run it. This chunk of code is just the 'run
it'
part.


okay -- it's probably worth trying it out (the default method isn' t
working
out for some reason anyway).

where does this code belong?

dim db as database
set db=currentdb
db.execute sql
set db=nothing

best,

-ted



:

No. Provided that the new record needs to be written with null
values
for
any of the other values (or 0s for numeric fields) then just leaving
them
out of the query will work fine. The actual format of how you
choose to
display those fields on a form is also immaterial - bear in mind
that
you're
not writing data to the form - that's just a means of viewing the
data -
it's going into a table.

Which of the methods you feel is simpler is always going to be down
to
you.
But I guarantee you 100% that the SQL method is a hugely powerful
technique
that has uses in many places in Access. It *is* much better and as
I
mentioned before, a great deal easier to debug.


in addition to the pn and ln (which comprise the PK), there are
five
other
controls on this same form. do they need referring to in the
append
query?
does it matter at all that both pn and ln are comboboxes; do they
need
to
get
changed into textboxes?

i still think that the "Default" 'method' is the simpler
conceptually
to
understand and the one that 'feels' the 'best'.

:

I'm not saying the default value method will never work... just
that
it
isn't the best way. Anyway - the SQL method:

Set up a new append query (not based on any table/query) that
will
add
some
values into the table your data would be adding to. Use any
values
in
that
query. Then switch to the SQL view of that query (via the View
menu).
It
will look something like...

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

You now need to build that same string but instead of the random
values
1
and 2... include the actual values you want to use. That will
be
something
like...

dim sql as string
dim pn as long
dim ln as long
pn=.... 'whatever.. set it to the patient number you want
ln=.... 'whatever... set it to the lesion number you want
sql="INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
"+
_
"SELECT "+cstr(pn)+" AS PN, "+cstr(ln)+" AS LN;"

Doing it that way you can check that pn and ln are getting
calculated
correctly, and by adding a breakpoint, check that the SQL
statement
works.... by copying and pasting it into a new query window.

You can then run it by...

dim db as database
set db=currentdb
db.execute sql
set db=nothing

The last part will be just to move to that newly added record.
But
I
wouldn't worry about that until you get the above working.


since i don't pretend to be the final word on vba (what would
i be
doing
asking all this stuff), what i think you're saying is that it
won't
work
on
the Default Property of Lesion Number no matter how much i
adhere
to
the
tenets of the other contributors to my posting....

you mentioned that you thought some SQL-izing would be
constructive.
can
you
flesh that out a little. what are you thinking
about....remember
this
is a
newbie on this end :)



:

It's viable... just a bit flaky and difficult to control (as
I
think
you've
found). Your expression [Me]![Patient Number] in your dmax
will
only
work
if the record has already been added and that leads to
timing
issues
which
is where I think your problem is.

If you want a record with a specific PN and lesion number
then
you're
better
off writing it directly. You get way more control and
debugability.


.....i forgot to propose an example of the code you had in
mind
(assuming
the
idea of setting expressions on the Default properties of
the
Patient
and
Lesion Number fields isn't viable).

-ted

:

When you click the 'New Record' button on the standard
record
navigation
bar
then, if the form is filtered, it doesn't automatically
carry
that
filtering
value on to the new record.

Where is the form opened from? It should (always) be
another
form
(or
forms) that specify the patient number. I'd tend to use
code on
that
(or
those) form(s) that adds a new record via an SQL
statement
(including
the PN
and the dmax+1) and then just go to that record.



i have an a2k application and a form having two
controls
which
make up
the
PK.
a) Patient Number
b) Lesion Number

the form is 'filtered' so when user designates a
patient
number to
work on
from another open form, he's taken to this one and can
view
only
those
PNs
selected.

there are multiple records viewable on this form (it's
coninuous)
each
one
having an incrementally larger value of "Lestion
Number"
(from
1,2,....,12)

i wrote a few vba pieces of code shown below

Private Sub Form_Load()
LAS_EnableSecurity Me
Me.Patient_Number.Value = Forms![RECIST Disease
Evaluation:
Nontarget
Lesions]![Patient Number]
End Sub

the above to handle the case when the form's loading
(and
was
written
for
the case when no records were entered prior)

how can i let user simultaneously duplicate a Patient
Number
and
increment
a
Lesion Number w/o having to do so manually.

Private Sub Patient_Number_AfterUpdate()
Me.Patient_Number.DefaultValue = "'" & [Patient
Number] &
"'"
'
that's
a
single quote within doubles...
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1
+
DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = "
&
Me![Patient
Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the
upper
limit",
vbInformation)
End If
DoCmd.Requery
End Sub

the above intended to duplicate the value of Patient
Number
and
increment
Lesion Number.

in testing this out when no other records matching the
PN
were
created, i
opened the form and found Patient Number filled in (by
the
'load'
vba
presumably).

the 'pencil' is on the first record and the Patient
Number's
completed.
the
'*' is on the record below. if i manually key in '01'
into
the
Lesion
Number
control it returns a run-time error '3058' abut index
or
pk
not
being
able
to contain a null -value and highligts the
DoCmd.Requery
in
the
vba
code
below

Private Sub Lesion_Number_AfterUpdate()
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"), 1, 1
+
DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number] = "
&
Me![Patient
Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding the
upper
limit",
vbInformation)
End If
DoCmd.Requery
End Sub


this is probably one of those 'really simple'
solutions
that
befuddle
the
newbie population.


but to further round things out, if i open a form with
a
bunch
of
records
already entered and click on 'add record'. i need to
manually
key
in
the
PN
when i click 'add record' at which instant it
automatically
fills
in
the
LN,
displays a record (w/ completed PN in the '*' row) and
causes
the
cursor
to
jump to the topmost (first record).

????
 
R

Rob Oldfield

Wahey!!!!!

Glad to hear it. I have go out for a while right now, but I'll see if I can
come up with the last piece of the jigsaw a bit later. Incidentally, I'd
suspect that (possibly) the reason the 'default value' method wasn't working
was that if you're using the asterisk then the record was being added before
the default values had been set (or something like that).

And to your other point of my reason for laying the code out as I did, then
yes you've pretty much got it. As I said a while ago, the idea of putting
an SQL statement together on the fly is wildly powerful because you can do
so much with it...

Run an 'action' query to update some data (as we're doing here)
Create a select command and use it as the basis of subform, listbox,
combobox, report, form.
Create a stored query from it.
....and various other things.


Ted said:
woops....i think i spoke in haste; i looked at the table underlying the form
and found, 'lo and behold, the records (blank in every respect but for the
values of Patient and Lesion numbers) that clicking the button i told you i'd
added must've generated :)

so it looks like we're converging!

the next thing you're going to undoubtedly tackle is displaying these
'hidden' records on the form.

-ted


Rob Oldfield said:
Exactly. I'd see it as a 'New' button which would add the record and take
you to it.

(...and what were you using to trigger the code before?)


Ted said:
well let's start off by seeing how much 'better' it's going to be, however
better is defined.

regarding the existence of a button. it doesn't, however if it'll get the
showboat moving i could create one -- which answers your question wrt what
other code there IS on it -- NONE.

since i'm writing this from home and w/o the aid and comfort of the database
itself, i'll have to begin it no sooner than manyana morning. but i
can
see
that even now i'm unclear as to where you would have this concoction
of
ours
belong. i mean you asked if i had a button established .... so as to
wrap
it
up in an event trigger/property?

-ted


:

I said that it was a better approach... not that it was easier... :)

Could I just check something... you have a button that you're
clicking
on a
form that is going to be triggering this routine? Does that button also
have any other code included? Anyway...

Basically, the SQL you are going to want to run is going to be of the
form...

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

....except for two things. The first one you already have -
tblTarget
is
indeed a surrogate for your actual table name. As your table name contains
spaces it will also need some additional square brackets... so it'd look
like...

INSERT INTO [Lesions: Non-Target] ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

The second thing is that if you run that SQL it's going to add a new record
into the table with 1 as the patient number and 2 as the lesion number...
which isn't what you want. You therefore need to build a string
similar
to
that one, but with the actual values you want instead of the 1 and
2.
More
comments on that below.

The last point is that isn't an SQL statement that you save. It's
going
to
be built as and when it's needed, with the correct values included,
and
then
run, and then discarded.

'Set up variables.
'sql is going to be the command to be run
'pn is going to be the patient number you want to add
'ln is going to be the lesion number you want to add
dim sql as string
dim pn as long
dim ln as long

'Figure out the required values
'pn=.... 'whatever.. set it to the patient number you want
'from looking at your other posts it will be something like...
pn=[Forms]![RECIST Disease Evaluation: Nontarget Lesions]![Patient Number]

'ln=.... 'whatever... set it to the lesion number you want
'again, from looking at your other posts, something like...
ln=Nz(DMax("[Lesion Number]","[Lesions: Non-Target]","[Patient
Number] =
"
& [Forms]![RECIST Disease Evaluation: Nontarget Lesions]![Patient
Number]),0) + 1

You could, at this point, set a new breakpoint so that you can check that
the correct values for pn and ln are being returned, or just drop in a
msgbox pn and/or msgbox ln if you prefer. It's definitely worth
doing
that
because if it isn't returning the correct values then the remaining
code
is
certainly not going to do what you want it to.

Next, you need to create the SQL statement - but containing the
values
you
want
That's done just by joining bits of text together....

sql="INSERT INTO [Lesions: Non-Target] ( [Patient Number], [Lesion Number] )
"& _
"SELECT "&cstr(pn)&" AS PN, "&cstr(ln)&" AS LN;"

Again, you should check here on what the sql string looks like. It should
be exactly the same as the one with the 1 and 2 in from before, but with
your values.

Last thing... just run that sql string. That's done like this...

dim db as database
set db=currentdb
db.execute sql
set db=nothing

That, as I mentioned a while ago, will hopefully add the correct new record.
The last part, which we haven't covered yet, is to move to that
newly
added
record, but let's leave that for the moment. For now you will hopefully
just be able to use the navigation buttons to find it.

Good luck!


well, i don't seem to be having all that much luck penetrating your
description of the process that this'd involve either....

here's where it's at:

i wrote the SQL

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

and saved it as "qryAppendToNonTargetLesions".

now, the trick seems to be decoding what you're saying.

is tblTarget just used as a surrogate for the real table's name, i.e.
"Lesions: Non-Target"

(btw, "Patient Number" is an eight digit integer valued number and "Lesion
Number" is a three digit integer valued number that can be no
greater
than
999)

when you say "You now need to build that same string but instead
of
the
random values 1 and 2....include the actual values you want to
use"
this
is
where you start to lose me.





:

In the order that it appeared in the previous post. Step 1 is
to
build
the
sql string, step 2 is to run it. This chunk of code is just the 'run
it'
part.


okay -- it's probably worth trying it out (the default method
isn'
t
working
out for some reason anyway).

where does this code belong?

dim db as database
set db=currentdb
db.execute sql
set db=nothing

best,

-ted



:

No. Provided that the new record needs to be written with null
values
for
any of the other values (or 0s for numeric fields) then just leaving
them
out of the query will work fine. The actual format of how you
choose to
display those fields on a form is also immaterial - bear in mind
that
you're
not writing data to the form - that's just a means of
viewing
the
data -
it's going into a table.

Which of the methods you feel is simpler is always going to
be
down
to
you.
But I guarantee you 100% that the SQL method is a hugely powerful
technique
that has uses in many places in Access. It *is* much better
and
as
I
mentioned before, a great deal easier to debug.


in addition to the pn and ln (which comprise the PK),
there
are
five
other
controls on this same form. do they need referring to in the
append
query?
does it matter at all that both pn and ln are comboboxes;
do
they
need
to
get
changed into textboxes?

i still think that the "Default" 'method' is the simpler
conceptually
to
understand and the one that 'feels' the 'best'.

:

I'm not saying the default value method will never
work...
just
that
it
isn't the best way. Anyway - the SQL method:

Set up a new append query (not based on any table/query) that
will
add
some
values into the table your data would be adding to. Use any
values
in
that
query. Then switch to the SQL view of that query (via
the
View
menu).
It
will look something like...

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

You now need to build that same string but instead of
the
random
values
1
and 2... include the actual values you want to use.
That
will
be
something
like...

dim sql as string
dim pn as long
dim ln as long
pn=.... 'whatever.. set it to the patient number you want
ln=.... 'whatever... set it to the lesion number you want
sql="INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
"+
_
"SELECT "+cstr(pn)+" AS PN, "+cstr(ln)+" AS LN;"

Doing it that way you can check that pn and ln are getting
calculated
correctly, and by adding a breakpoint, check that the SQL
statement
works.... by copying and pasting it into a new query window.

You can then run it by...

dim db as database
set db=currentdb
db.execute sql
set db=nothing

The last part will be just to move to that newly added record.
But
I
wouldn't worry about that until you get the above working.


since i don't pretend to be the final word on vba
(what
would
i be
doing
asking all this stuff), what i think you're saying is
that
it
won't
work
on
the Default Property of Lesion Number no matter how much i
adhere
to
the
tenets of the other contributors to my posting....

you mentioned that you thought some SQL-izing would be
constructive.
can
you
flesh that out a little. what are you thinking
about....remember
this
is a
newbie on this end :)



:

It's viable... just a bit flaky and difficult to
control
(as
I
think
you've
found). Your expression [Me]![Patient Number] in
your
dmax
will
only
work
if the record has already been added and that leads to
timing
issues
which
is where I think your problem is.

If you want a record with a specific PN and lesion number
then
you're
better
off writing it directly. You get way more control and
debugability.


.....i forgot to propose an example of the code
you
had in
mind
(assuming
the
idea of setting expressions on the Default
properties
of
the
Patient
and
Lesion Number fields isn't viable).

-ted

:

When you click the 'New Record' button on the standard
record
navigation
bar
then, if the form is filtered, it doesn't automatically
carry
that
filtering
value on to the new record.

Where is the form opened from? It should
(always)
be
another
form
(or
forms) that specify the patient number. I'd
tend to
use
code on
that
(or
those) form(s) that adds a new record via an SQL
statement
(including
the PN
and the dmax+1) and then just go to that record.



i have an a2k application and a form having two
controls
which
make up
the
PK.
a) Patient Number
b) Lesion Number

the form is 'filtered' so when user designates a
patient
number to
work on
from another open form, he's taken to this one
and
can
view
only
those
PNs
selected.

there are multiple records viewable on this
form
(it's
coninuous)
each
one
having an incrementally larger value of "Lestion
Number"
(from
1,2,....,12)

i wrote a few vba pieces of code shown below

Private Sub Form_Load()
LAS_EnableSecurity Me
Me.Patient_Number.Value = Forms![RECIST Disease
Evaluation:
Nontarget
Lesions]![Patient Number]
End Sub

the above to handle the case when the form's loading
(and
was
written
for
the case when no records were entered prior)

how can i let user simultaneously duplicate a Patient
Number
and
increment
a
Lesion Number w/o having to do so manually.

Private Sub Patient_Number_AfterUpdate()
Me.Patient_Number.DefaultValue = "'" & [Patient
Number] &
"'"
'
that's
a
single quote within doubles...
Me.Lesion_Number = IIf(IsNull("[Lesion
Number]"),
1, 1
+
DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient
Number]
= "
&
Me![Patient
Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records
exceeding
the
upper
limit",
vbInformation)
End If
DoCmd.Requery
End Sub

the above intended to duplicate the value of Patient
Number
and
increment
Lesion Number.

in testing this out when no other records
matching
the
PN
were
created, i
opened the form and found Patient Number
filled in
(by
the
'load'
vba
presumably).

the 'pencil' is on the first record and the Patient
Number's
completed.
the
'*' is on the record below. if i manually key
in
'01'
into
the
Lesion
Number
control it returns a run-time error '3058'
abut
index
or
pk
not
being
able
to contain a null -value and highligts the
DoCmd.Requery
in
the
vba
code
below

Private Sub Lesion_Number_AfterUpdate()
Me.Lesion_Number = IIf(IsNull("[Lesion
Number]"),
1, 1
+
DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient
Number]
= "
&
Me![Patient
Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records
exceeding
the
upper
limit",
vbInformation)
End If
DoCmd.Requery
End Sub


this is probably one of those 'really simple'
solutions
that
befuddle
the
newbie population.


but to further round things out, if i open a
form
with
a
bunch
of
records
already entered and click on 'add record'. i
need
to
manually
key
in
the
PN
when i click 'add record' at which instant it
automatically
fills
in
the
LN,
displays a record (w/ completed PN in the '*'
row)
and
causes
the
cursor
to
jump to the topmost (first record).

????
 
G

Guest

regarding your observations/conjectures wrt the "*" default approach i'd
taken, it seemed to make 'gut' sense to this writer but i guess those dudes
in washington state who put this environment together have other ideas about
how it oughta work.

will wait for your next ramblings w/ baited breath :)

-ted


Rob Oldfield said:
Wahey!!!!!

Glad to hear it. I have go out for a while right now, but I'll see if I can
come up with the last piece of the jigsaw a bit later. Incidentally, I'd
suspect that (possibly) the reason the 'default value' method wasn't working
was that if you're using the asterisk then the record was being added before
the default values had been set (or something like that).

And to your other point of my reason for laying the code out as I did, then
yes you've pretty much got it. As I said a while ago, the idea of putting
an SQL statement together on the fly is wildly powerful because you can do
so much with it...

Run an 'action' query to update some data (as we're doing here)
Create a select command and use it as the basis of subform, listbox,
combobox, report, form.
Create a stored query from it.
....and various other things.


Ted said:
woops....i think i spoke in haste; i looked at the table underlying the form
and found, 'lo and behold, the records (blank in every respect but for the
values of Patient and Lesion numbers) that clicking the button i told you i'd
added must've generated :)

so it looks like we're converging!

the next thing you're going to undoubtedly tackle is displaying these
'hidden' records on the form.

-ted


Rob Oldfield said:
Exactly. I'd see it as a 'New' button which would add the record and take
you to it.

(...and what were you using to trigger the code before?)


well let's start off by seeing how much 'better' it's going to be, however
better is defined.

regarding the existence of a button. it doesn't, however if it'll get the
showboat moving i could create one -- which answers your question wrt what
other code there IS on it -- NONE.

since i'm writing this from home and w/o the aid and comfort of the
database
itself, i'll have to begin it no sooner than manyana morning. but i can
see
that even now i'm unclear as to where you would have this concoction of
ours
belong. i mean you asked if i had a button established .... so as to wrap
it
up in an event trigger/property?

-ted


:

I said that it was a better approach... not that it was easier... :)

Could I just check something... you have a button that you're clicking
on a
form that is going to be triggering this routine? Does that button also
have any other code included? Anyway...

Basically, the SQL you are going to want to run is going to be of the
form...

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

....except for two things. The first one you already have - tblTarget
is
indeed a surrogate for your actual table name. As your table name
contains
spaces it will also need some additional square brackets... so it'd look
like...

INSERT INTO [Lesions: Non-Target] ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

The second thing is that if you run that SQL it's going to add a new
record
into the table with 1 as the patient number and 2 as the lesion
number...
which isn't what you want. You therefore need to build a string similar
to
that one, but with the actual values you want instead of the 1 and 2.
More
comments on that below.

The last point is that isn't an SQL statement that you save. It's going
to
be built as and when it's needed, with the correct values included, and
then
run, and then discarded.

'Set up variables.
'sql is going to be the command to be run
'pn is going to be the patient number you want to add
'ln is going to be the lesion number you want to add
dim sql as string
dim pn as long
dim ln as long

'Figure out the required values
'pn=.... 'whatever.. set it to the patient number you want
'from looking at your other posts it will be something like...
pn=[Forms]![RECIST Disease Evaluation: Nontarget Lesions]![Patient
Number]

'ln=.... 'whatever... set it to the lesion number you want
'again, from looking at your other posts, something like...
ln=Nz(DMax("[Lesion Number]","[Lesions: Non-Target]","[Patient Number] =
"
& [Forms]![RECIST Disease Evaluation: Nontarget Lesions]![Patient
Number]),0) + 1

You could, at this point, set a new breakpoint so that you can check
that
the correct values for pn and ln are being returned, or just drop in a
msgbox pn and/or msgbox ln if you prefer. It's definitely worth doing
that
because if it isn't returning the correct values then the remaining code
is
certainly not going to do what you want it to.

Next, you need to create the SQL statement - but containing the values
you
want
That's done just by joining bits of text together....

sql="INSERT INTO [Lesions: Non-Target] ( [Patient Number], [Lesion
Number] )
"& _
"SELECT "&cstr(pn)&" AS PN, "&cstr(ln)&" AS LN;"

Again, you should check here on what the sql string looks like. It
should
be exactly the same as the one with the 1 and 2 in from before, but with
your values.

Last thing... just run that sql string. That's done like this...

dim db as database
set db=currentdb
db.execute sql
set db=nothing

That, as I mentioned a while ago, will hopefully add the correct new
record.
The last part, which we haven't covered yet, is to move to that newly
added
record, but let's leave that for the moment. For now you will hopefully
just be able to use the navigation buttons to find it.

Good luck!


well, i don't seem to be having all that much luck penetrating your
description of the process that this'd involve either....

here's where it's at:

i wrote the SQL

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

and saved it as "qryAppendToNonTargetLesions".

now, the trick seems to be decoding what you're saying.

is tblTarget just used as a surrogate for the real table's name, i.e.
"Lesions: Non-Target"

(btw, "Patient Number" is an eight digit integer valued number and
"Lesion
Number" is a three digit integer valued number that can be no greater
than
999)

when you say "You now need to build that same string but instead of
the
random values 1 and 2....include the actual values you want to use"
this
is
where you start to lose me.





:

In the order that it appeared in the previous post. Step 1 is to
build
the
sql string, step 2 is to run it. This chunk of code is just the
'run
it'
part.


okay -- it's probably worth trying it out (the default method isn'
t
working
out for some reason anyway).

where does this code belong?

dim db as database
set db=currentdb
db.execute sql
set db=nothing

best,

-ted



:

No. Provided that the new record needs to be written with null
values
for
any of the other values (or 0s for numeric fields) then just
leaving
them
out of the query will work fine. The actual format of how you
choose to
display those fields on a form is also immaterial - bear in mind
that
you're
not writing data to the form - that's just a means of viewing
the
data -
it's going into a table.

Which of the methods you feel is simpler is always going to be
down
to
you.
But I guarantee you 100% that the SQL method is a hugely
powerful
technique
that has uses in many places in Access. It *is* much better and
as
I
mentioned before, a great deal easier to debug.


in addition to the pn and ln (which comprise the PK), there
are
five
other
controls on this same form. do they need referring to in the
append
query?
does it matter at all that both pn and ln are comboboxes; do
they
need
to
get
changed into textboxes?

i still think that the "Default" 'method' is the simpler
conceptually
to
understand and the one that 'feels' the 'best'.

:

I'm not saying the default value method will never work...
just
that
it
isn't the best way. Anyway - the SQL method:

Set up a new append query (not based on any table/query)
that
will
add
some
values into the table your data would be adding to. Use any
values
in
that
query. Then switch to the SQL view of that query (via the
View
menu).
It
will look something like...

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

You now need to build that same string but instead of the
random
values
1
and 2... include the actual values you want to use. That
will
be
something
like...

dim sql as string
dim pn as long
dim ln as long
pn=.... 'whatever.. set it to the patient number you want
ln=.... 'whatever... set it to the lesion number you want
sql="INSERT INTO tblTarget ( [Patient Number], [Lesion
Number] )
"+
_
"SELECT "+cstr(pn)+" AS PN, "+cstr(ln)+" AS LN;"

Doing it that way you can check that pn and ln are getting
calculated
correctly, and by adding a breakpoint, check that the SQL
statement
works.... by copying and pasting it into a new query window.

You can then run it by...

dim db as database
set db=currentdb
db.execute sql
set db=nothing

The last part will be just to move to that newly added
record.
But
I
wouldn't worry about that until you get the above working.


since i don't pretend to be the final word on vba (what
would
i be
doing
asking all this stuff), what i think you're saying is that
it
won't
work
on
the Default Property of Lesion Number no matter how much i
adhere
to
the
tenets of the other contributors to my posting....

you mentioned that you thought some SQL-izing would be
constructive.
can
you
flesh that out a little. what are you thinking
about....remember
this
is a
newbie on this end :)



:

It's viable... just a bit flaky and difficult to control
(as
I
think
you've
found). Your expression [Me]![Patient Number] in your
dmax
will
only
work
if the record has already been added and that leads to
timing
issues
which
is where I think your problem is.

If you want a record with a specific PN and lesion
number
then
you're
better
off writing it directly. You get way more control and
debugability.



.....i forgot to propose an example of the code you
had in
mind
(assuming
the
idea of setting expressions on the Default properties
of
the
Patient
and
Lesion Number fields isn't viable).

-ted

:

When you click the 'New Record' button on the
standard
record
navigation
bar
then, if the form is filtered, it doesn't
automatically
carry
that
filtering
value on to the new record.

Where is the form opened from? It should (always)
be
another
form
(or
forms) that specify the patient number. I'd tend to
use
code on
that
(or
those) form(s) that adds a new record via an SQL
statement
(including
the PN
and the dmax+1) and then just go to that record.


message

i have an a2k application and a form having two
controls
which
make up
the
PK.
a) Patient Number
b) Lesion Number

the form is 'filtered' so when user designates a
patient
number to
work on
from another open form, he's taken to this one and
can
view
only
those
PNs
selected.

there are multiple records viewable on this form
(it's
coninuous)
each
one
having an incrementally larger value of "Lestion
Number"
(from
1,2,....,12)

i wrote a few vba pieces of code shown below

Private Sub Form_Load()
LAS_EnableSecurity Me
Me.Patient_Number.Value = Forms![RECIST Disease
Evaluation:
Nontarget
Lesions]![Patient Number]
End Sub

the above to handle the case when the form's
loading
(and
was
written
for
the case when no records were entered prior)

how can i let user simultaneously duplicate a
Patient
Number
and
increment
a
Lesion Number w/o having to do so manually.

Private Sub Patient_Number_AfterUpdate()
Me.Patient_Number.DefaultValue = "'" & [Patient
Number] &
"'"
'
that's
a
single quote within doubles...
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"),
1, 1
+
DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number]
= "
&
Me![Patient
Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding
the
upper
limit",
vbInformation)
End If
DoCmd.Requery
End Sub

the above intended to duplicate the value of
Patient
Number
and
increment
Lesion Number.

in testing this out when no other records matching
the
PN
were
created, i
opened the form and found Patient Number filled in
(by
the
'load'
vba
presumably).

the 'pencil' is on the first record and the
Patient
Number's
completed.
the
'*' is on the record below. if i manually key in
'01'
into
the
Lesion
Number
control it returns a run-time error '3058' abut
index
or
pk
not
being
able
to contain a null -value and highligts the
DoCmd.Requery
in
the
vba
code
below

Private Sub Lesion_Number_AfterUpdate()
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"),
1, 1
+
DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number]
= "
&
Me![Patient
Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding
the
upper
limit",
vbInformation)
End If
DoCmd.Requery
End Sub


this is probably one of those 'really simple'
solutions
that
befuddle
the
newbie population.


but to further round things out, if i open a form
with
a
bunch
of
records
already entered and click on 'add record'. i need
to
manually
key
in
the
PN
when i click 'add record' at which instant it
automatically
fills
in
the
LN,
displays a record (w/ completed PN in the '*' row)
and
causes
the
cursor
to
jump to the topmost (first record).

????
 
R

Rob Oldfield

The trouble with the "*" is that it will add a new record immediately i.e.
before your code has a chance to kick in and set default values.

(I think so anyway, I haven't set up a form that actually used it in so long
that I don't really remember.)

Anyway, all you will need at the end of the previous code will be...

dim rs as recordset
set rs=me.recordset.clone
rs.findfirst "[Patient Number]="&cstr(pn)&" and [Lesion Number]="&cstr(ln)
me.bookmark=rs.bookmark

Any good?


Ted said:
regarding your observations/conjectures wrt the "*" default approach i'd
taken, it seemed to make 'gut' sense to this writer but i guess those dudes
in washington state who put this environment together have other ideas about
how it oughta work.

will wait for your next ramblings w/ baited breath :)

-ted


Rob Oldfield said:
Wahey!!!!!

Glad to hear it. I have go out for a while right now, but I'll see if I can
come up with the last piece of the jigsaw a bit later. Incidentally, I'd
suspect that (possibly) the reason the 'default value' method wasn't working
was that if you're using the asterisk then the record was being added before
the default values had been set (or something like that).

And to your other point of my reason for laying the code out as I did, then
yes you've pretty much got it. As I said a while ago, the idea of putting
an SQL statement together on the fly is wildly powerful because you can do
so much with it...

Run an 'action' query to update some data (as we're doing here)
Create a select command and use it as the basis of subform, listbox,
combobox, report, form.
Create a stored query from it.
....and various other things.


Ted said:
woops....i think i spoke in haste; i looked at the table underlying
the
form
and found, 'lo and behold, the records (blank in every respect but for the
values of Patient and Lesion numbers) that clicking the button i told
you
i'd
added must've generated :)

so it looks like we're converging!

the next thing you're going to undoubtedly tackle is displaying these
'hidden' records on the form.

-ted


:

Exactly. I'd see it as a 'New' button which would add the record
and
take
you to it.

(...and what were you using to trigger the code before?)


well let's start off by seeing how much 'better' it's going to be, however
better is defined.

regarding the existence of a button. it doesn't, however if it'll
get
the
showboat moving i could create one -- which answers your question
wrt
what
other code there IS on it -- NONE.

since i'm writing this from home and w/o the aid and comfort of the
database
itself, i'll have to begin it no sooner than manyana morning. but
i
can
see
that even now i'm unclear as to where you would have this
concoction
of
ours
belong. i mean you asked if i had a button established .... so as
to
wrap
it
up in an event trigger/property?

-ted


:

I said that it was a better approach... not that it was
easier...
:)
Could I just check something... you have a button that you're clicking
on a
form that is going to be triggering this routine? Does that
button
also
have any other code included? Anyway...

Basically, the SQL you are going to want to run is going to be
of
the
form...

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

....except for two things. The first one you already have - tblTarget
is
indeed a surrogate for your actual table name. As your table name
contains
spaces it will also need some additional square brackets... so
it'd
look
like...

INSERT INTO [Lesions: Non-Target] ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

The second thing is that if you run that SQL it's going to add a new
record
into the table with 1 as the patient number and 2 as the lesion
number...
which isn't what you want. You therefore need to build a string similar
to
that one, but with the actual values you want instead of the 1
and
2.
More
comments on that below.

The last point is that isn't an SQL statement that you save.
It's
going
to
be built as and when it's needed, with the correct values
included,
and
then
run, and then discarded.

'Set up variables.
'sql is going to be the command to be run
'pn is going to be the patient number you want to add
'ln is going to be the lesion number you want to add
dim sql as string
dim pn as long
dim ln as long

'Figure out the required values
'pn=.... 'whatever.. set it to the patient number you want
'from looking at your other posts it will be something like...
pn=[Forms]![RECIST Disease Evaluation: Nontarget Lesions]![Patient
Number]

'ln=.... 'whatever... set it to the lesion number you want
'again, from looking at your other posts, something like...
ln=Nz(DMax("[Lesion Number]","[Lesions: Non-Target]","[Patient Number] =
"
& [Forms]![RECIST Disease Evaluation: Nontarget Lesions]![Patient
Number]),0) + 1

You could, at this point, set a new breakpoint so that you can check
that
the correct values for pn and ln are being returned, or just
drop in
a
msgbox pn and/or msgbox ln if you prefer. It's definitely worth doing
that
because if it isn't returning the correct values then the
remaining
code
is
certainly not going to do what you want it to.

Next, you need to create the SQL statement - but containing the values
you
want
That's done just by joining bits of text together....

sql="INSERT INTO [Lesions: Non-Target] ( [Patient Number], [Lesion
Number] )
"& _
"SELECT "&cstr(pn)&" AS PN, "&cstr(ln)&" AS LN;"

Again, you should check here on what the sql string looks like. It
should
be exactly the same as the one with the 1 and 2 in from before,
but
with
your values.

Last thing... just run that sql string. That's done like this...

dim db as database
set db=currentdb
db.execute sql
set db=nothing

That, as I mentioned a while ago, will hopefully add the correct new
record.
The last part, which we haven't covered yet, is to move to that newly
added
record, but let's leave that for the moment. For now you will hopefully
just be able to use the navigation buttons to find it.

Good luck!


well, i don't seem to be having all that much luck penetrating your
description of the process that this'd involve either....

here's where it's at:

i wrote the SQL

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

and saved it as "qryAppendToNonTargetLesions".

now, the trick seems to be decoding what you're saying.

is tblTarget just used as a surrogate for the real table's
name,
i.e.
"Lesions: Non-Target"

(btw, "Patient Number" is an eight digit integer valued number and
"Lesion
Number" is a three digit integer valued number that can be no greater
than
999)

when you say "You now need to build that same string but
instead
of
the
random values 1 and 2....include the actual values you want to use"
this
is
where you start to lose me.





:

In the order that it appeared in the previous post. Step 1
is
to
build
the
sql string, step 2 is to run it. This chunk of code is just the
'run
it'
part.


okay -- it's probably worth trying it out (the default
method
isn'
t
working
out for some reason anyway).

where does this code belong?

dim db as database
set db=currentdb
db.execute sql
set db=nothing

best,

-ted



:

No. Provided that the new record needs to be written
with
null
values
for
any of the other values (or 0s for numeric fields) then just
leaving
them
out of the query will work fine. The actual format of
how
you
choose to
display those fields on a form is also immaterial - bear
in
mind
that
you're
not writing data to the form - that's just a means of viewing
the
data -
it's going into a table.

Which of the methods you feel is simpler is always going
to
be
down
to
you.
But I guarantee you 100% that the SQL method is a hugely
powerful
technique
that has uses in many places in Access. It *is* much
better
and
as
I
mentioned before, a great deal easier to debug.


in addition to the pn and ln (which comprise the PK), there
are
five
other
controls on this same form. do they need referring to
in
the
append
query?
does it matter at all that both pn and ln are
comboboxes;
do
they
need
to
get
changed into textboxes?

i still think that the "Default" 'method' is the simpler
conceptually
to
understand and the one that 'feels' the 'best'.

:

I'm not saying the default value method will never work...
just
that
it
isn't the best way. Anyway - the SQL method:

Set up a new append query (not based on any table/query)
that
will
add
some
values into the table your data would be adding to.
Use
any
values
in
that
query. Then switch to the SQL view of that query
(via
the
View
menu).
It
will look something like...

INSERT INTO tblTarget ( [Patient Number], [Lesion Number] )
SELECT 1 AS PN, 2 AS LN;

You now need to build that same string but instead
of
the
random
values
1
and 2... include the actual values you want to use. That
will
be
something
like...

dim sql as string
dim pn as long
dim ln as long
pn=.... 'whatever.. set it to the patient number you want
ln=.... 'whatever... set it to the lesion number you want
sql="INSERT INTO tblTarget ( [Patient Number], [Lesion
Number] )
"+
_
"SELECT "+cstr(pn)+" AS PN, "+cstr(ln)+" AS LN;"

Doing it that way you can check that pn and ln are getting
calculated
correctly, and by adding a breakpoint, check that
the
SQL
statement
works.... by copying and pasting it into a new query window.

You can then run it by...

dim db as database
set db=currentdb
db.execute sql
set db=nothing

The last part will be just to move to that newly added
record.
But
I
wouldn't worry about that until you get the above working.


since i don't pretend to be the final word on vba (what
would
i be
doing
asking all this stuff), what i think you're saying
is
that
it
won't
work
on
the Default Property of Lesion Number no matter
how
much i
adhere
to
the
tenets of the other contributors to my posting....

you mentioned that you thought some SQL-izing would be
constructive.
can
you
flesh that out a little. what are you thinking
about....remember
this
is a
newbie on this end :)



:

It's viable... just a bit flaky and difficult to control
(as
I
think
you've
found). Your expression [Me]![Patient Number]
in
your
dmax
will
only
work
if the record has already been added and that
leads
to
timing
issues
which
is where I think your problem is.

If you want a record with a specific PN and lesion
number
then
you're
better
off writing it directly. You get way more
control
and
debugability.



.....i forgot to propose an example of the
code
you
had in
mind
(assuming
the
idea of setting expressions on the Default properties
of
the
Patient
and
Lesion Number fields isn't viable).

-ted

:

When you click the 'New Record' button on the
standard
record
navigation
bar
then, if the form is filtered, it doesn't
automatically
carry
that
filtering
value on to the new record.

Where is the form opened from? It should (always)
be
another
form
(or
forms) that specify the patient number. I'd tend to
use
code on
that
(or
those) form(s) that adds a new record via an SQL
statement
(including
the PN
and the dmax+1) and then just go to that record.


message

i have an a2k application and a form
having
two
controls
which
make up
the
PK.
a) Patient Number
b) Lesion Number

the form is 'filtered' so when user
designates
a
patient
number to
work on
from another open form, he's taken to this
one
and
can
view
only
those
PNs
selected.

there are multiple records viewable on
this
form
(it's
coninuous)
each
one
having an incrementally larger value of "Lestion
Number"
(from
1,2,....,12)

i wrote a few vba pieces of code shown below

Private Sub Form_Load()
LAS_EnableSecurity Me
Me.Patient_Number.Value = Forms![RECIST Disease
Evaluation:
Nontarget
Lesions]![Patient Number]
End Sub

the above to handle the case when the form's
loading
(and
was
written
for
the case when no records were entered prior)

how can i let user simultaneously duplicate a
Patient
Number
and
increment
a
Lesion Number w/o having to do so manually.

Private Sub Patient_Number_AfterUpdate()
Me.Patient_Number.DefaultValue = "'" & [Patient
Number] &
"'"
'
that's
a
single quote within doubles...
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"),
1, 1
+
DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number]
= "
&
Me![Patient
Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding
the
upper
limit",
vbInformation)
End If
DoCmd.Requery
End Sub

the above intended to duplicate the value of
Patient
Number
and
increment
Lesion Number.

in testing this out when no other records matching
the
PN
were
created, i
opened the form and found Patient Number filled in
(by
the
'load'
vba
presumably).

the 'pencil' is on the first record and the
Patient
Number's
completed.
the
'*' is on the record below. if i manually
key
in
'01'
into
the
Lesion
Number
control it returns a run-time error '3058' abut
index
or
pk
not
being
able
to contain a null -value and highligts the
DoCmd.Requery
in
the
vba
code
below

Private Sub Lesion_Number_AfterUpdate()
Me.Lesion_Number = IIf(IsNull("[Lesion Number]"),
1, 1
+
DMax("[Lesion
Number]", "Lesions: Non-Target", "[Patient Number]
= "
&
Me![Patient
Number]))
If Me![Lesion Number] > 10 Then
RetValue = MsgBox("Delete any records exceeding
the
upper
limit",
vbInformation)
End If
DoCmd.Requery
End Sub


this is probably one of those 'really simple'
solutions
that
befuddle
the
newbie population.


but to further round things out, if i open
a
form
with
a
bunch
of
records
already entered and click on 'add record'.
i
need
to
manually
key
in
the
PN
when i click 'add record' at which instant it
automatically
fills
in
the
LN,
displays a record (w/ completed PN in the
'*'
row)
and
causes
the
cursor
to
jump to the topmost (first record).

????
 

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