"Invalid use of null" for OpenArgs when calling OpenForm - vba

I cannot find the reason but I keep getting an invalid use of null when using OpenArgs
The code is set up with
Private Sub cmdContactDetails_Click()
Dim ArgsString As String
ArgsString = Forms!LeadList![Entrepreneur Name] & "," & Forms!LeadList![Telephone Number] & "," & Forms!LeadList![Email Address]
DoCmd.OpenForm "ContactDetails", , , , , , ArgsString
End Sub
This results in the string "X,123456789,test#test"
In the Form_Open event of contact details I have:
Dim SaveString As String
SaveString = Forms!ContactDetails.OpenArgs
But this gives me an "Invalid Use of Null" message even though nothing is null.

Assuming you have a form named:- "LeadList"
and the form also has a text box named:- "Entrepreneur_Name"
and this form contains the following code:-
Option Compare Database
Option Explicit
Private Sub cmdContactDetails_Click()
Dim ArgsString As String
ArgsString = Me.Entrepreneur_Name
DoCmd.OpenForm "ContactDetails", , , , , , ArgsString
End Sub
The above code is called via a command button named "cmdContactDetails" on the form named:- "LeadList" then the form "ContactDetails" will be opened (form "ContactDetails" must be closed first) and the on open event will run this code:-
Option Compare Database
Option Explicit
Private Sub Form_Open(Cancel As Integer)
Dim SaveString As String
SaveString = Forms!ContactDetails.OpenArgs
MsgBox " >>> " & SaveString
End Sub
The message box will display the contents of the text transferred via the open args function from the form "LeadList" text box "Entrepreneur_Name"

Related

prevent duplicates when passing values between two forms (with Args)

I have two forms: transfert Form with its subform and intransfert Form. I am using
DoCmd.OpenForm "intransfert", , , , acFormAdd, acDialog, Me!Text83
(where text83 is =[transfertasubform].[Form]![transfertadetailid] under
Private Sub Command78_Click()
in transfet form and
Private Sub Form_Load()
On Error Resume Next
If Me.NewRecord Then Me!trnrin = Me.OpenArgs
in intransfet form. intransfert form is based in transfertdetailquery. i wont to prevent passing text83 value more then one time
i tried to explain my problem and expect a help to prevent duplicates when used Arge
assuming trnrin is the name of a variable in your record source. assuming you mean that you want to avoid adding two records where trnrin has the same value and the user sent the same open args twice. assuming trnrin is also the name of a control in the detail section of the intransfert form.
'form load only runs when the form is opened so you have to close the form to pass new args
'but you can just move the same code to a button or whatever
Private Sub IntransferForm_Load()
If Len(Me.OpenArgs) > 0 Then
Me.txtBoxintheHeader = Me.OpenArgs 'the load event is the right place to set controls
'most of this code is to check if there is already an instance where trnrin = the OpenArgs in the record source
Dim lookedupArgs As String
lookedupArgs = Nz(lookedupArgs = DLookup("trnrin", "MyTable", "trnrin = " & "'" & Me.OpenArgs & "'"), "ValuethatisneveranArg")
If lookedupArgs = "ValuethatisneveranArg" Then
'Debug.Print "trnrin = '" & Me.OpenArgs & "'" 'note the string delimiters
'Me.trnrin = Me.OpenArgs 'this surprisingly works but will break easily and doesn't handle complex cases
Dim rs As Recordset
Set rs = Me.Recordset 'if recordset is bound to a table then table will also be updated.
'you can also bind to the table directly but you may need to call me.requery to show the changes
rs.AddNew
rs!trnrin = Me.OpenArgs
rs.Update
End If
End If
End Sub

Data type mismatch in criteria expression error while trying to filter report

I just started with Access not too long ago and I'm having some trouble.
I am trying to filter a report that displays tracking numbers and associated data scanned in from barcodes on boxes. The tracking number is the primary key and it is stored as Short Text in the table.
I have a command button set up in a form that launches an input box to type or scan in the tracking number.
Whenever try to run this I get a
Run-time error '3464: Data type mismatch in the criteria expression.
My code:
Private Sub Command28_Click()
Dim ReportName As String
ReportName = "bucketContents"
Dim trackingNum As String
trackingNum = InputBox("Enter the Tracking number:", "Tracking Number Input")
'if a value was entered, open and filter report
If Len(Trim(trackingNum)) > 0 Then
DoCmd.OpenReport ReportName, acViewPreview, , "[sampleID] = " & trackingNum
End If
End Sub
Text values must be wrapped in quotes:
DoCmd.OpenReport ReportName, acViewPreview, , "[sampleID] = '" & trackingNum & "'"

Is there any way to change the properties of a Ms Access dialog Form when is Opened?

Instead of using msgBox, I want to create My msgBox by form, "frmMsg".
"frmMsg" form has tow bottom, (Ok and No), and a label(lblMsg) for show message.
"frmMsg" property: Pop Up = Yes , Modal = Yes.
My Function for Open form is MsgInfo:
Public Function MsgInfo(Optional msg As String = "Are You Ok?", _
Optional msgCaption As String = "Warning" ) As Boolean
MsgInfo = False
DoCmd.OpenForm "frmMsg"
Form_frmMsg.Caption = msgCaption ' Set Caption of Form
Form_frmMsg.lblMsg.Caption = msg ' Set Message of Form
MsgInfo = MsgInfoResult ' MsgInfoResult is Public Variable to store MsgInfo Result (Ok Bottom(True) or No Bottom(False) )
End Function
I used this in other Form, Example For delete Customer in Customer List ( Delete Bottom ):
Private Sub btnDelete_Click()
DoCmd.SetWarnings False
If MsgInfo("Are You Sure Delete Customer?", , "Delete Customer!") = True Then
' Run SQL for Delete Customer
Dim sqlDelete As String
sqlDelete = "DELETE tblCustomer.*, tblCustomer.RowId " & _
"FROM tblCustomer " & _
"WHERE tblCustomer.RowId=[Forms]![frmCustomerList]![frmCustomerListSub]![RowId]"
DoCmd.RunSQL sqlDelete
Form_frmCustomerList.frmCustomerListSub.Requery
End If
DoCmd.SetWarnings True
End Sub
My Problem is After Open MsgInfo Form Before the user answers this, the Next commands (Sql) are executed.
To solve the problem, I changed AcWindowsMode in Function MsgInfo:
DoCmd.OpenForm "frmMsg"
to
DoCmd.OpenForm "frmMsg", , , , , acDialog
problem solved but There was another problem. The following commands are not executed:
Form_frmMsg.Caption = msgCaption ' Set Caption of Form
Form_frmMsg.lblMsg.Caption = msg ' Set Message of Form
MsgInfo = MsgInfoResult ' MsgInfoResult is Public Variable
please help me.
I am no Expert in VBA or Any Programming Language neither am I a programmer by Profession. But , I've had my fair share in dealing with programming languages as a hobby.
In VBA, Any Code after the ,,,,,acDialog can't be run until the Form is closed so , What you need to do is find a way to Pass the Messages somewhere and have the form Retrieve it.
Create a Module to Pass the Message from the Function to the Form
'a Module named Module_gVar
Public MessageHeader as String 'Optional A Separate Text Box For a Header
Public MessageBody as String 'Main Body
Public MessageTitle as String 'Caption of the Form
Public MessageReturn as Boolean
This is the function to Call the Message box and Get a simple True or False Return
'Function To Call the MessageBox
Public Function CallMessageBox ( _
Optional msgHeader as string , _
Optional msgBody as string , _
Optional msgTitle as string)
Module_gVar.MessageTitle = msgTitle
Module_gVar.MessageHeader = msgHeader
Module_gVar.MessageBody = msgBody
DoCmd.OpenForm "frmMessage",,,,,acDialog
CallMessageBox = Module_gVar.MessageReturn
'You can have the CleanUp on a Separate Function
'Since it's not a Procedure Variable it Isn't cleaned when it goes out of scope
Module_gVar.MessageTitle = ""
Module_gVar.MessageBody = ""
Module_gVar.MessageHeader = ""
Module_gVar.Return = False
End Function
Now for the Form itself.
'Retrieve the Strings
Private Sub Form_Current()
Me.[YourHeaderTextBox] = Module_gVar.MessageHeader
Me.[YourBodyTextBox] = Module_gVar.MessageBody
Me.Caption = Module_gVar.MessageTitle
End Sub
'A Button to Return a Value
Private Sub cmdYes_Click() ' Yes button
Module_gVar.MessageReturn = True
DoCmd.Close acForm,"frmMessage",acSaveNo
End Sub
Private Sub cmdNo_Click() ' No Button
Module_gVar.MessageReturn = False
Docmd.Close acForm,"frmMessage",acSaveNo
End Sub
You can Optimize the code from here on but it's the basic structure. I Recommend creating a Class Module in which you can retrieve strings , input strings , and call Forms.

Enter Parameter Value Dialog Box when opening form

I have a form open and I have set the ID number up as a hyperlink to another form. I have created the code below to open up however it now comes up with the Enter Parameter Value dialog box. When I put the ID number it opens correctly.
Private Sub StudentID_Click()
Dim recordID As Integer
recordID = Me.StudentID
DoCmd.OpenForm "Students Extended", , , "Studentid=StudentNumber"
End Sub
I guess you are after something like this:
Private Sub StudentID_Click()
Dim recordID As Long
recordID = Me.StudentID
DoCmd.OpenForm "Students Extended", , , "Studentid = " & recordID & ""
End Sub

VB open form on a specific record

I am using Microsoft access and i want to open a form to a specific id when a button is clicked. Can i specify the id in the Do.open form command. the code below opens a form but then a dialog box opens asking to enter the id . anyone any ideas ?
Private Sub button1_Enter()
Dim recordID As Integer
recordID = Me.CurrentRecord
DoCmd.OpenForm "Form3", , , "ID = recordID"
End sub
First, change:
recordID = Me.CurrentRecord
To:
recordID = Me.ID
Where Me.ID is the name of a field or control on the current form ( Microsoft Access - get record id when button is clicked ).
When you refer to a variable, put it outside the quotes:
DoCmd.OpenForm "Form3", , , "ID = " & recordID
This is going to be fine for an ID, which is numeric, but it will get a little more complicated with text and dates, because you will need delimiters. This will work well as long as sometextvar does not contain a quote:
DoCmd.OpenForm "Form3", , , "Atext = '" & sometextvar & "'"
Otherwise
DoCmd.OpenForm "Form3", , , "Atext = '" & Replace(sometextvar,"'","''") & "'"
And dates take #
DoCmd.OpenForm "Form3", , , "ADate = #" & somedatevar & "#"
To avoid problems with locales, it is nearly always best to format to year, month, day, so:
DoCmd.OpenForm "Form3", , , "ADate = #" & Format(somedatevar,"yyyy/mm/dd") & "#"
I tried I had the same, be sure that the name you give to the id is the name used in the form, not in the DB! IE, my id is id in DB, but pc_id in my form!
How about this solution?
Private Sub Command10_Click()
On Error GoTo Err_Command10_Click
Dim IDnr As String
Dim stDocName As String
Dim stLinkCriteria As String
IDnr = InputBox("Enter ID")
stDocName = "FORM_MASTER"
stLinkCriteria = "[ID]=" & IDnr
DoCmd.OpenForm stDocName, , , stLinkCriteria
Exit_Command10_Click:
Exit Sub
Err_Command10_Click:
MsgBox Err.Description
Resume Exit_Command10_Click
End Sub
Here's what has worked for me when I want to open a form (call it the Destination form) from another form (call it the Launch form) AND I want the records viewed in Destination form to be filtered (i.e., restricted) to only the record that was current in the Launch form AND the field that I want to use for filtering the records is of a text (or "string") data type:
Private Sub cmdOpenDestinationForm
DoCmd.OpenForm "frmDestination",,,"[FieldName]=" & "'" & Me.ControlName & "'"
End Sub
[FieldName] refers a field in the Destination form. [ControlName] refers to a control (like a text box) on the Launch form. The "Me" also refers to the Launch form, because that's the form that is open when you click the command button (and you could omit it). In effect, this code is telling Access to (1) open frmDestination, (2) Search through the "FieldName" field until you find a record that has the same value as that in Me.ControlName, and (3) Filter the Destination form so as to show only that one record.
The tricky part are all those double quotes ( " ), single quotes ( ' ) and ampersands (&).
Note that [FieldName]= has opening and closing double quotes: "[FieldName]=". And don't forget the = symbol. Next, this is followed by & "'". That's an ampersand, a double quote, a single quote, and another double quote. In my experience, you cannot have spaces between the quote symbols. They must be run together as shown. Then this is followed by another ampersand and the name of the control on the Launch form that has the value you want to locate in [FieldName] on the Destination form: & Me.ControlName. And, as stated before, you can omit the Me if you want. I use it because helps me remember that ControlName is a control on the Launch form, not the Destination form. Then this is followed by yet another & and another set of the double and single quotes: "'".