Enter Parameter Value Dialog Box when opening form - vba

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

Related

Open form based on text value

I am looking to try and have a text box that i type in a number and i want it to open another form based on that number. So i have form frmNonMgr that i type a number in the text field txtPhone and i want it to open frmFolder_Tabs to the record that relates to that number. [Phone] in the employees table are unique and you cannot have two of the same, when ive gotten it to work its just opening the last record or a blank record.
Private Sub btnFolder_Click()
If IsNull(Me.txtPhone) Then
MsgBox "Please enter a Phone"
Me.txtPhone.SetFocus
Else
DoCmd.OpenForm "frmFolder_Tabs", , , "[Phone]='" & Me.txtPhone
DoCmd.Close acForm, "frmNonMgr"
End If
End Sub
It would be better to store phone number to a variable and then close the active form first. Then open your target form and use filter with stored variable. Also for string type data you need to enclose value with single quote what Kostas already mentioned. Try below codes.
Private Sub btnFolder_Click()
Dim strPhone As String
If IsNull(Me.txtPhone) Then
MsgBox "Please enter a Phone"
Me.txtPhone.SetFocus
Else
strPhone = Me.txtPhone
DoCmd.Close
DoCmd.OpenForm "frmFolder_Tabs", , , "[Phone]='" & strPhone & "'"
End If
End Sub

I created a modal dialog to jump to a record in a report but I can only search for one record

I created a modal pop up with a textbox, search and a print button. The functionality is the user types an ID in the textbox the search button finds the ID in the report, the print button prints the record, everything works fine until I try and type a new ID in and click search, nothing happens. Why?
Public stuID As String
Private Sub printProfile_Click()
DoCmd.OpenReport "toBeFiltered", acViewPreview, , "[stuIDOriginal] = " & stuID
DoCmd.PrintOut acSelection
End Sub
Private Sub searchID_Click()
stuID = stuIDTxt.Value
DoCmd.OpenReport "toBeFiltered", acViewPreview, , "[stuIDOriginal] = " & stuID
End Sub
An Access Report's Print Preview is a static snapshot of rendered output and not dynamic interface, hence it will not reflect a changed filter nor can it be searched on.
Consider changing the view to Report View to change the filter and then snapshot the report to Print Preview (but remove the filter on final view):
Public stuID As String
Private Sub printProfile_Click()
DoCmd.OpenReport "toBeFiltered", acViewReport, , "[stuIDOriginal] = " & stuID
DoCmd.OpenReport "toBeFiltered", acViewPreview
DoCmd.PrintOut acSelection
End Sub
Private Sub searchID_Click()
stuID = stuIDTxt.Value
DoCmd.OpenReport "toBeFiltered", acViewReport, , "[stuIDOriginal] = " & stuID
DoCmd.OpenReport "toBeFiltered", acViewPreview
End Sub

"Invalid use of null" for OpenArgs when calling OpenForm

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"

Me.Dirty moves currently selected record

I have an Access 2007 form that's giving me some headache.
I have a list of records that start as a combo box to pick a company and then a series of checkboxes to indicate the company's role.
If the user needs to add a new company, they'd pick the company name from the combo box, and then click the checkbox that indicates what role the company is playing. When the checkbox is checked, the form follows the following code to show a popup that captures additional information:
Private Sub chkOperationsAndMaintenance_AfterUpdate()
'DoCmd.RunCommand acCmdSaveRecord'
If Me.Dirty Then Me.Dirty = False
If chkOperationsAndMaintenance.Value = True Then
DoCmd.OpenForm "OmPopupForm", , , "CompanyProjectId = " & Me.CompanyProjectID, , acDialog
Me.Requery
End If
End Sub
This code will save the record on the CompanyProject table to create the row. It then pulls the CompanyProjectID (PK of the CompanyProject table) so it knows which ID to feed the popup.
The issue I'm having is that on the Me.Dirty line (and also the above commented-out acCmdSaveRecord), the entire form saves and refreshes, moving the selected row to the first record (in this case, "Gamesa") rather than the newly entered record. So the ensuing popup is fed the CompanyProjectID of the first record rather than the newly-entered record, and it also reads the "checked" state of the checkbox from the first record rather than the one upon which the user is working.
I doctored the code to look like this:
Private Sub chkOperationsAndMaintenance_AfterUpdate()
Dim CPID As Long
Dim CID As Long
Dim rs As Recordset
Set rs = Me.RecordsetClone
CID = Me.CompanyID
'DoCmd.RunCommand acCmdSaveRecord'
If Me.Dirty Then Me.Dirty = False
CPID = DLookup("MAX(CompanyProjectID)", "CompanyProject", "cpProjectID = " & Me.cpProjectID & _
" AND CompanyID = " & CID)
rs.Find "[CompanyProjectID] = " & CPID
Me.Bookmark = rs.Bookmark
If chkOperationsAndMaintenance.Value = True Then
DoCmd.OpenForm "OmPopupForm", , , "CompanyProjectId = " & Me.CompanyProjectID, , acDialog
Me.Requery
End If
End Sub
The idea with these alterations is that we'll get the FK from the parent form ("cpProjectID") and the CompanyID of the selection from the combobox, then save the record. Once the record is saved, we have the CompanyProjectID already stored, so then the rs.Find and Me.Bookmark lines will then select the record that matches that CompanyProjectID.
This sometimes works, and sometimes doesn't. Generally doesn't. In this case, I refreshed the parent form and the current form and attempted to add a new company and then click the "Owner" checkbox (which uses the same code as above, just a different checkbox ID) to see the Bookmark on the wrong row:
At this point, I'm not sure what to do. If I don't save the record (via acCmdSaveRecord or Me.Dirty=False) then I don't have a CompanyProjectID to send as the input parameter for the ensuing popup, but if I do save the record, then the form changes the record and the wrong parameter is sent and the wrong checkbox's checked state is read. I can't just use an arbitrary index to work off of (such as acNewRec), as the user might need to edit an existing row instead of adding a new one.
I tried the method in this post already: MS Access how to Update current row, move to next record, not first, but the find/bookmark isn't consistently working.
EDIT (12/15/2014)
I ended up going with the following VBA code:
Private Sub chkOperationsAndMaintenance_AfterUpdate()
Dim CPID As Long
Dim CID As Long
Dim rs As Recordset
Set rs = Me.RecordsetClone
CID = Me.CompanyID
If Me.Dirty Then Me.Dirty = False
CPID = DLookup("MAX(CompanyProjectID)", "CompanyProject", "cpProjectID = " & Me.cpProjectID & _
" AND CompanyID = " & CID)
Do While Me.CompanyProjectID <> CPID
If Me.CurrentRecord < Me.Recordset.RecordCount Then
DoCmd.GoToRecord , , acNext
Else
DoCmd.GoToRecord , , acFirst
End If
Loop
If chkOperationsAndMaintenance.Value = True Then
DoCmd.OpenForm "OmPopupForm", , , "CompanyProjectId = " & Me.CompanyProjectID, , acDialog
Me.Requery
End If
End Sub
I feel like there has to be a better answer for this, but this is appearing to work right now. I have my end user/lab rat testing it at the moment, so I'll mark this complete if this pans out. I still think there has to be a better solution for this, but at the moment, this solution is able to capture the requisite ID to pass to the popup, and it selects the appropriate row after this ID is captured.
Have you considered saving the current position - like Me.CurrentRecord, then after the save reposition to the desired record. As an example, you could add a 'Before' or 'After' Insert to save the position, then later reposition. See the following:
Option Compare Database
Option Explicit
Dim lCurRec As Long
Private Sub Form_AfterInsert()
lCurRec = Me.CurrentRecord
Debug.Print "After Insert, Current: " & Me.CurrentRecord
End Sub
<<< YOUR SUB>>>
DoCmd.GoToRecord acDataForm, Me.Name, acGoTo, lCurRec
This code will work when adding a new record as well as when editing an existing record:
Private Sub chkOperationsAndMaintenance_AfterUpdate()
Dim c As Long
Me.Dirty = False
If chkOperationsAndMaintenance Then
DoCmd.OpenForm "OmPopupForm", , , "CompanyProjectId = " & Me.CompanyProjectID, , acDialog
c = Me.CurrentRecord
Me.Requery
DoCmd.GoToRecord , , acGoTo, c
End If
End Sub
I am guessing that your popup form does things that are specific to Operations & Maintenance, and does not change the values of any of the checkboxes on the main form. If this is the case, you don't need a Requery at all, and the code can be simplified:
Private Sub chkOperationsAndMaintenance_AfterUpdate()
Me.Dirty = False
If chkOperationsAndMaintenance Then
DoCmd.OpenForm "OmPopupForm", , , "CompanyProjectId = " & Me.CompanyProjectID, , acDialog
End If
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: "'".