automatic transfer of data from one form to another

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have scanned through the many questions, tried many of the suggestions but
I still can't get data to automatically transfer from one form to another
using a button. My problem:

Patient data is entered in form 1 and an initial infection number is
recorded. When a button is pressed a new form opens (form 2) for additional
data entry. In a blank record (in form 2) the original infection number
(from form 1) needs to automatically appear if there has been a prior entry
the all information should appear. There is a one-to-many relation from form
1 to form 2.

I can automatically pull up the infection number if prior data has already
been entered but can't get the infection number field to automatically fill
if there hasn't been a prior entry. HELP??? I have included my last coding
attempt on the button (on click)....

DoCmd.OpenForm "Form2", acNormal, , , acFormAdd
Forms!Form1.InfectNo = Me.InfectNo
DoCmd.Close Me.Name
 
You need to do three things:

Firstly make sure that Form1's current record is saved as if this is a new
record being added via Form1 the addition of a new record via Form2 would
violate referential integrity as there would not yet be a match in From1's
underlying table. As you are closing Form1 this step is probably not
required in fact, as that will save the record.

Secondly, filter Form2 to the InfectNo of Form1's current record so it shows
existing data for the current patient.

Thirdly set the DefaultValue property of the InfectNo control in Form2 to
the current value of InfectNo in Form1. You can do this by passing the value
by means of the OpenArgs mechanism.

Assuming the linking field InfectNo is a number data type the code to open
Form2 would thus be:

RunCommand acCmdSaveRecord
DoCmd.OpenForm "Form2", _
WhereCondition:= "InfectNo = " & Me.InfectNo, _
OpenArgs:= Me.InfectNo
DoCmd.Close acForm, Me.Name

In Form2's Open event procedure set the DefaultValue property of the
InfectNo control to that passed to it via the OpenArgs mechanism, testing for
Null in case Form2 has been opened independently:

If Not IsNull(Me.OpenArgs) Then
Me.InfectNo.DefaultValue = """" & Me.OpenArgs & """"
End If

Note that the value for the DefaultValue property is wrapped in quotes.
Regardless of the data type involved this property is always a string
expression so is delimited with quotes. Most of the time they could be
omitted, but sometimes they are essential when you might not expect it.
Dates are a case in point as a date such as 07/04/2005 would be interpreted
as an arithmetical expression if not delimited. Its prudent to use them
every time therefore.
 
I guess I am not as Access "Savy" as I thought...I tried what you wrote but I
keep getting an error message "This property is read-only and can't be set"
when I press the button to open form 2. The error message originates from
the WhereCondition and OpenArgs.

I am also confused on where to put the openargs mechanism in form 2.

Can you use a little more beginner language for me?
 
As Einstein said about explaining the Theory of General Relativity "I can
make it as simple as possible, but no more" <G>.

Before doing so, however, can you copy and paste here the code exactly as
you've entered it into the button's event procedure. It sounds to me like
there may be some, probably small, error in the syntax.
 
Thanks in advanced for any help you can give me. :-)

Ken Sheridan said:
As Einstein said about explaining the Theory of General Relativity "I can
make it as simple as possible, but no more" <G>.

Private Sub OpenCheckSheet_Click()
On Error GoTo Err_OpenCheckSheet_Click

RunCommand acCmdSaveRecord
DoCmd.OpenForm "CheckSheetUTI", acNormal, , , acFormAdd
WhereCondition = "Sign_Organism_No" & Me.Sign_Organism_No
OpenArgs = Me.Sign_Organism_No
DoCmd.Close acForm, Me.Name

If Not IsNull(Me.OpenArgs) Then
Me.Sign_Organism_No.DefaultValue = """" & Me.OpenArgs & """"

Exit_OpenCheckSheet_Click:
Exit Sub

Err_OpenCheckSheet_Click:
MsgBox Err.DESCRIPTION
Resume Exit_OpenCheckSheet_Click

End If

End Sub
 
Where you've gone wrong is in treating one line in the example code I sent
you as three separate lines. The underscore character at the end of a line
of code is a continuation character and means that the following line is
actually a continuation of the current line. This makes the code more easily
readable as you can see the whole line, split up over several lines, in the
width of the VBA editor window. WhereCondition and OpenArgs are arguments of
the OpenForm method. The first one filters the form being opened, the second
one passes a value to the form. I've named them specifically, which is why
they are followed by := in the code rather than just relying on their
positions in the argument list and putting in loads of commas (the colon
before the = sign is essential when you do this). Again this makes for
greater readability of the code. Notice also that I've not used the
acFormAdd setting because, if I understand you rightly, you want to show any
existing record(s) in the CheckSheetUTI form which match the current record
in the first form. If there are no matching records then the CheckSheetUTI
form would open at a new record.

The other mistake you've made is to put the code which should go in the
CheckSheetUTI form's Open event procedure in the Click event procedure of the
OpenCheckSheet button on the first form.

So, the code for the button's Click event procedure should go like this:

Private Sub OpenCheckSheet_Click()

On Error GoTo Err_OpenCheckSheet_Click

RunCommand acCmdSaveRecord
DoCmd.OpenForm "CheckSheetUTI", _
WhereCondition:="Sign_Organism_No" & Me.Sign_Organism_No, _
OpenArgs:=Me.Sign_Organism_No
DoCmd.Close acForm, Me.Name

Exit_OpenCheckSheet_Click:
Exit Sub

Err_OpenCheckSheet_Click:
MsgBox Err.DESCRIPTION
Resume Exit_OpenCheckSheet_Click

End Sub

The other code goes in the Open event procedure of form CheckSheetUTI. To
do this open the form in design view and make sure the Properties Sheet is
open by selecting Properties from the View menu if its not. In the combo box
at the top of the properties sheet select Form from the drop down list.
Select the Event tab in the properties sheet and then select the On Open
event property. Click on the 'build' button (the one on the right with 3
dots). Select Code Builder in the dialogue which opens. The VBA window
should now open at the form's Open event procedure with the first and last
lines of the procedure already in place. Add the following 3 lines of code
between these existing lines to set the Sign_Organism_No control's
DefaultValue property to the value you passed from the first form:

If Not IsNull(Me.OpenArgs) Then
Me.Sign_Organism_No.DefaultValue = """" & Me.OpenArgs & """"
End If

What should now happen when you click the button on the first form, if there
are any records in the CheckSheetUTI form's underlying table which match the
current Sign_Organism_No in the first form, then the from will open with just
those record(s) (and the usual blank form for adding a new record). If there
are no matching records then the form will open at a blank record for you to
add a new one with the Sign_Organism_No value from the first form already in
place. Until you add some more data to the form it won't be 'Dirty' as the
value in Sign_Organism_No is just a default value until you start to add
more data, so at this stage you can abandon adding a new record simply by
closing the form if you wish. If you add more data, however, the new record
will be saved when you close the form and the Sign_Organism_No field will
have the value you passed to it.

I hope I've followed Einstein's dictum and made it as simple as possible,
but no more so. If you have any problem's however, post back here. At first
reading it might sound a bit scary, but if you take it step by step you
should be OK. After doing this sort of thing a few times it becomes a piece
of cake, and takes less time to do it than it has taken me to describe it.
Let me know how you get on.
 
I am pretty sure I did exactly what was described below. You did a great job
explaining what was going on and no, the instructions weren't intimidating,
just realize I have a lot to learn about Visual Basic.... The good news is
that the record number from the first form is now automatically going into a
blank CheckSheet UTI form. The bad news is that matching records in the
CheckSheetUTI form do not appear. Any thoughts? I attached my formulas once
again....

Button's Properties:
Private Sub OpenUTIForm_Click()
On Error GoTo Err_OpenUTIForm_Click

RunCommand acCmdSaveRecord
DoCmd.OpenForm "CheckSheetUTI2", _
WhereCondition:="Sign_Organism_No" & Me.Sign_Organism_No, _
OpenArgs:=Me.Sign_Organism_No
DoCmd.Close acForm, Me.Name

Exit_OpenUTIForm_Click:
Exit Sub

Err_OpenUTIForm_Click:
MsgBox Err.DESCRIPTION
Resume Exit_OpenUTIForm_Click

End Sub

Form's Open Properties:
Private Sub Form_Open(Cancel As Integer)
If Not IsNull(Me.OpenArgs) Then
Me.Sign_Organism_No.DefaultValue = """" & Me.OpenArgs & """"
End If
End Sub



shfe
 
Mea culpa! I'd missed out the equals sign. The line should be:

DoCmd.OpenForm "CheckSheetUTI2", _
WhereCondition:="Sign_Organism_No = " & Me.Sign_Organism_No, _
OpenArgs:=Me.Sign_Organism_No

While we're at it lets just make sure one other base is covered; I've
assumed that Sign_Organism_No is a number data type, but if it happened to
be a text data type rather than a number, then its value would need to be
wrapped in quotes when you build the expression for the WhereCondition
argument of the OpenForm method. To include literal quotes in a string
already delimited by quotes you can use a contiguous pair of quotes, so the
relevant line would be:

DoCmd.OpenForm "CheckSheetUTI2", _
WhereCondition:="Sign_Organism_No = """ & Me.Sign_Organism_No &
"""", _
OpenArgs:=Me.Sign_Organism_No

If the Sign_Organism_No value were ABC1234 for instance the expression for
the WhereCondition argument would evaluate to Sign_Organism_No = "ABC1234".
For the field to be a text data type it doesn't have to include non-numeric
characters of course; it could still be text with a value "1234".

For the OpenArgs argument you don't have to do the same because in that case
you are assigning a value to the argument, not building a string expression,
which would explain why the default value is being passed to the form
successfully even if the value happened to be a text data type.
 

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

Back
Top