sending 2 variables through openargs

cjamps

Registered User.
Local time
Today, 07:55
Joined
Feb 2, 2009
Messages
29
I have a combo on a form that has contains serial numbers. If the user selects the appropriate serial number a form opens for the record to be edited. If the serial number does not exist the user adds it to the combo and a form opens so the user can add the rest of the information. Opening the same form how can I pass "sn" to the form if the record has to be edited or "newdata" if the record has to be added?

'The code that is run after the selected record is chosen from the combo Private Sub cmbDSerialNumber_AfterUpdate() Me.RecordsetClone.FindFirst "[DSerialNumber] = '" & Me!cmbDSerialNumber & "'"
Me.Bookmark = Me.RecordsetClone.Bookmark End Sub

Private Sub EditDocketBtn_Click()
Dim sn As String
sn = Me.cmbDSerialNumber
DoCmd.OpenForm "DocketsFrm", acNormal, , , acFormEdit, acDialog, sn End Sub

Private Sub Form_Load()
If Not IsNull(Me.OpenArgs) Then
Dim sn As String
sn = Me.OpenArgs
Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone
rs.FindFirst "DSerialNumber = '" & sn & "'"
If Not rs.NoMatch Then
Me.Bookmark = rs.Bookmark
End If
End If
Me.Caption = "Serial Number: " & Me!DSerialNumber End Sub

This is my NotInList Procedure. How do I pass NewData in OpenArgs as well?

Private Sub CMBDSerialNumber_NotInList(NewData As String, Response As Integer) Dim Result Dim Msg As String Dim CR As String

CR = Chr$(13)

' Exit this subroutine if the combo box was cleared.
If NewData = "" Then Exit Sub

Msg = "'" & NewData & "' is not in the list." & CR & CR Msg = Msg & "Do you want to add it?"
If MsgBox(Msg, vbQuestion + vbYesNo) = vbYes Then DoCmd.OpenForm "DocketsFrm", , , , acAdd, acDialog, NewData End If

Result = DLookup("[DSerialNumber]", "Dockets", _ "[DSerialNumber]='" & NewData & "'") If IsNull(Result) Then Response = acDataErrContinue

Else
Response = acDataErrAdded
End If
End Sub
 
Firstly when posting code, in future could you please use the "Code" tag that's the Hash (#) button at the top of the dialogue box, it will make thing easier to follow.

You don't need to send two variables in the OpenArgs. As you are sending the SN in the OpenArgs, a new part will not have a SN, so all you need to do is a logical test on the OpenArgs in the new forms OnOpen event and then get the form to respond accordingly. If the OpenArgs are null you know you have an new part other wise you have an existing part.
 
"If the OpenArgs are null you know you have an new part other wise you have an existing part."

Please excuse me but I do not understand what that means. The openargs will never be null because either newdata from the NOTINLIST procedure is being passed or the sn (cmbdserialnumber) is being passed to the form.
 
what i normally do is store indicators etc of this tyoe in global variables, and examine them in the calling form

this sort of thing

clear variables
open form
wait, while form is open
examine variables

then you can detect whether anything needs doing from the variable settings
 
"If the OpenArgs are null you know you have an new part other wise you have an existing part."

Please excuse me but I do not understand what that means. The openargs will never be null because either newdata from the NOTINLIST procedure is being passed or the sn (cmbdserialnumber) is being passed to the form.

From this statmenet in the OP
cjamps said:
....Opening the same form how can I pass "sn" to the form if the record has to be edited or "newdata" if the record has to be added?.....

I had assumed that you are opening your form either to add new data, in which case the OpenArg would be null, or you are opening the form to edit existing data in which case your OpenArgs will contain a serial number.

It would however seem on further reading that I have missed some vital details :o

Not with standing this why do you need the user to add the SN to the Combo why not add it in the form whilst you are adding all the other details?
 
I have a combo on my main form with serial numbers. After selecting the appropriate record the user can choose to do a host of activities by clicking the appropriate command button. I felt that it was neater to choose the serial number from the main form and pass it over rather than having the same combo on several forms. I like to combo idea that if the user is not sure whether the serial number exists they could start typing. If it does then they select it, if it doesn't then they add it.

Isn't there anything like a conditional openargs. For example IIF(NOTINLIST = True newdata = openargs, sn = openargs)? I still don't understand how openargs can be null if newdata is being passed to it.

Thank you for your time and effort.
 
Here's another thought I had.

If the SN is, say, a six digit number, you could add 1,000,000 to it to indicate a new record and 2,000,000 to indicate an existing record. In your opened form you could use something like;

Code:
If Int(OpenArgs/1000000)=1 Then
     MsgBox "This Is a New Record"
Else
    MsgBox "This Is an Old Record"
End If

If however SN is a text string you could simply concatenate an "N"to the front to indicate a new record or an "O"for an old record then use something like;

Code:
If Left(OpenArgs,1)="N" Then
     MsgBox "This Is a New Record"
Else
    MsgBox "This Is an Old Record"
End If
 
Of course the manipulation of the SN would happen as part of the code that is causing your new form to open and then be passed into the OpenArgs. The code in my previous post would then run in the new forms OnLoad event. You would of course have to replace the MsgBox with whatever the appropriate action should be.
 
if you pass a value into openargs, then openargs isnt null

openargs is just a string - what the string contains and how you dereferencesit, is up to you - Its just standard to test for null first

eg you can use the null test to set some suitable values for testing, by just opening the form directly
 
if you pass a value into openargs, then openargs isnt null

openargs is just a string - what the string contains and how you dereferencesit, is up to you - Its just standard to test for null first

eg you can use the null test to set some suitable values for testing, by just opening the form directly

Hi Dave you are correct. I was thinking that if the SN had not yet been entered then the OpenArgs would be Null. However having just tested, OpenArgs will not hold a null value, it will only accept non nulls. At any rate there seem to be a couple of solutions to the problem expressed in the OP :)
 
you CAN have a null openargs. I just put this at the top of an open event in any form to test it - and to demonstrate the usage i thought possible

Code:
If IsNull(OpenArgs) Then
    'just to test the situation
    MsgBox ("openargs is null")
End If

If Nz(OpenArgs, vbNullString) = vbNullString Then
   'use this bit to set some values if you like, while testing
    MsgBox ("No OpenArgs")
Else
    'use this bit to analyse openargs if necessary
    MsgBox ("Openargs is " & OpenArgs)
End If
 
This message board is really great. I am a newbie at ms access and since it is not feasible for me to take a course I am trying to teach myself solely by using the internet. I hope one day to get a job in using ms access, right now I am just practicing.

The following:

If Left(OpenArgs,1)="N" Then
MsgBox "This Is a New Record"
Else
MsgBox "This Is an Old Record"
End If
was an interesting solution but I did not see how the following was possible so I just copied the same form and I use one for adding records (passing newdata) and one for editing (passing sn). I know it defeated the purpose because I wanted to use as little forms as possible but I appreciate the time and effort you put into it.
 
Dave yes once again you are correct. However that's not quite what I was aiming at.

I believe (and I could well be incorrect here) it is not possible to assign a Null value to the OpenArgs, this does not diminish the fact that OpenArgs can be Null (because it has not been specified in the DoCmd.OpenForm command), I just don't think it is possible to assign a null value to OpenArgs using;

Code:
DoCmd.OpenForm "Frm_Name" , , , , , , Me.ItemID

where Me.ItemID is an Autonumber that has not yet been assigned when the DoCmd.OpenForm is called. I've tried this and get a data type mismatch error. I've also tried a couple of other things but get an invalid us of null error.
 

Users who are viewing this thread

Back
Top Bottom