Access, Opening the Same Form in Different ways using Buttons - forms

I'm using Access 2007 at work and trying to build a database. I'd like to know if it's possible to have two different buttons on the "Main Menu" form that open the same "Data Entry" form. BUT, one button automatically goes to a new blank record for data entry, and the other button prompts the user to enter a specific ID # (tied to a field in the form) and then the form will open on that record. This would be for updating that specific record.
Is this possible? I am a beginner with VBA code. If this is possible with later versions of Access but not 2007, please let me know.

Create two buttons on the form. Let us name them as addRecBtn and openRecBtn let us say the form you are trying to open is tmpFrmName. So the first button will open the form in Data Entry Mode, the second one will open the form in normal edit mode.
The code should be something like,
Private Sub addRecBtn_Click()
DoCmd.OpenForm "tmpFrmName", DataMode:=acFormAdd
End Sub
The second form will first have to get the number you are trying to open, for example let us call it the numberID. So your code would be.
Private Sub openRecBtn_Click()
Dim numID As Long
numID = InputBox("Please enter the ID : ")
If DCount("*", "yourTable", "numberID = " & numID) <> 0 Then
DoCmd.OpenForm "tmpFrmName", WhereCondition:="numberID = " & numID
Else
MsgBox "The numberID : " & numID & " does not exist, please try again !"
End If
End Sub

You may be able to use DoCmd.OpenForm with different options to accomplish both your goals.
"one button automatically goes to a new blank record for data entry"
DoCmd.OpenForm FormName:="YourForm", View:=acNormal, DataMode:=acFormAdd
"the other button prompts the user to enter a specific ID # (tied to a field in the form) and then the form will open on that record"
Dim strWhere As String
strWhere = "[id]=" & Me.txtId
Debug.Print strWhere ' <- inspect this in the Immediate window
DoCmd.OpenForm FormName:="YourForm", View:=acNormal, WhereCondition=strWhere
The first form includes a text box named txtId, which contains the ID value you want to make current in the second form (YourForm).

Yes, it's possible.
My advice would be to do it with several functions - put the "shared" functionality in one (eg open the form), and then for the differences, make two functions which call the shared one
eg
function openForm()
{
//Do open form stuff
}
function promptForID()
{
openForm();
//Do stuff that prompts for ID
}
function blankRecord()
{
openForm();
//Do stuff that sets up a blank record
}

Related

Open Form to a value selected on ComboBox - Ms Acces

I want to open a form to a specific record based on the value is selected on a comboBox. I have written a code and its working, but before opening the form it shows a dialogue box with input field asking for the parameter i want for the form which i dont want the VB code to ask.
DoCmd.OpenForm "Final_Exam", acNormal, , "[admclass] = " & Me.Combo4.Value & ""
This is the code i have written and the requirement is that on clicking the button the form opens without any dialogue box asking for parameter. thanksa
What I've found is the best way to open one form from another is to use OpenArgs. When opening Form 2 from Form 1's button, use code like this:
Private Sub cmdOpenOtherForm_Click()
DoCmd.OpenForm FormName:="frmOtherForm", OpenArgs:=Me.Combo4.Value
End Sub
Then, in the Load event of Form 2, use the openargs to set up your filter:
Private Sub Form_Load()
If Not IsNull(Me.OpenArgs) Then
Me.Filter="[admclass]=""" & Me.OpenArgs & """"
Me.FilterOn = True
End If
End Sub
If the field you're filtering on is a text field, make sure to properly escape double quotes (as is done above).

Access Search Subform + Update Query Subform?

So I'm new to Access 365, and I'm working on a database for my church's cemetery. I have a "main" form that will house searches and the results of said searches (https://i.imgur.com/v6ZmDx2.png).
So I'd like to put a box on this form that when I press the "Run Query" button, updates with all records matching the criteria. I have the query working, and if I go into the individual search form and search, it works fine.
If I try dragging the query onto the main form, I get a SearchQuery subform, but when I go into form view, I have to fill out data before the form even loads. I need it to only update after I click the button, so that I can enter info, THEN search (https://i.imgur.com/6jJ416o.png & https://i.imgur.com/XPPpg6S.png). If I click cancel, it loads fine, but when I type in data to the search then press "Run Query", it still prompts me for the info (https://i.imgur.com/FKbZ6WK.png).
Thanks in advance!
Your code is constructing criteria with control names as the data to search in, must use table field names. Remove the Result_ from each expression.
Coffin and Cremation radio buttons are within an OptionGroup control. This means you must test for the value of the OptionGroup, not the controls within the group. Clicking radio button sets value of the OptionGroup control. Following is revised code for the OptionGroup and checkboxes. Note removal of Result_ - fix the other conditional expressions as well. Might want to rename the OptionGroup control.
'Coffin/Cremation
If Not IsNull(Me.optGroup) Then strFilter = strFilter & "([Coffin/Cremation] = " & Me.optGroup & ") AND "
'Veteran
If Not IsNull(Me.Veteran) Then strFilter = strFilter & "([Veteran] = " & Me.Veteran & ") AND "
'Monument
If Not IsNull(Me.Monument) Then strFilter = strFilter & "([Monument] = " & Me.Monument & ") AND "
Why is the Clear Search button using EmbeddedMacro? Code to clear search parameters:
Private Sub cmdReset_Click()
'Purpose: Clear all the search boxes in the Form Header, and show all records again.
Dim ctl As Control
'Clear all the controls in the Form Header section.
For Each ctl In Me.Section(acHeader).Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acCheckBox, acOptionGroup
ctl.Value = Null
End Select
Next
'Remove the form's filter.
Me.FilterOn = False
End Sub
Consider using comboboxes instead of textboxes to help users enter valid data. Can still allow any input and use LIKE/wildcard. RowSource SQL could be like:
SELECT DISTINCT [Last Name] FROM A ORDER BY [Last Name];
Advise not to use space nor punctuation/special characters (only exception is underscore) in naming convention.
You can access any form from the subform as follows:
MainForm: LOAN SETTLEMENT FORM
ex:
Forms![LOAN SETTLEMENT FORM]!txtEMP_ID.Value = Me.EMP_ID.Value
Forms![LOAN SETTLEMENT FORM]!txtEMP_NAME.Value = Me.EMP_NAME.Value

How to apply two filters to a split form to robustly update records in a table

I am storing data on eye colour, for example. I have a table about containing 100's of contact details for people in many different local authorities (LA). These contact details change frequently and a team of users will update the table when necessary.
I want to make a split form to do this for us. The idea is to be able to use a drop-down menu for both the LA (location) and the eye colour in the form, to filter the table below to show only the relevant contact details.
The user can then quickly find the correct record, click on it, this will autofill the rest of the form, and any details can be changed and then saved.
My problem is that I can't make the form robust enough to not erroneously overprint existing contact details, and I can't get a two step filter process to work. The contact details for previous records don't seem to clear, even when filtering for different places, so it is easy to muddle up and overprint records.
Here is a screen shot of the form so far:
example of the split form, filtered for records from Broadland but not by colour yet
Here is the VBA
Option Compare Database
Private Sub Detail_Click()
End Sub
Private Sub LA_AfterUpdate()
Me.Filter = "[LA] = " & Chr(34) & Me.LA & Chr(34)
Me.FilterOn = True
End Sub
Private Sub UpdateRecord_Click()
RunCommand acCmdSaveRecord
Me.Requery
End Sub
If anyone could help with:
How do I filter for colour as well as place?
How can I make the form save the amended record and then clear the form so that no records are overprinted?
If I filter for one thing and click to bring up the full record, how can I make the form drop this record and change to a different one if I decide to re-filter and pick a different record
I would be very grateful! Thank you in advance.
Answer for question 1
1.Open form Design View
2.Click at the table at the bottom of your form
3.Open Property Sheet for that table
4.Open Data at Property Sheet. You will see Link Master Fields and Link Child Fields. Click at ... sign and it will appear Subform Field Linker. Fill in the master and child field's column as you desired (eg. LA, color, place). Then the form will show the result.
Answer for Question 2
You have to use vba code under UPDATE RECORD button. For clearing the form after saving the record, use DoCmd.OpenForm "your form name",,,,acFormAdd
Example answer for question 2:
Private Sub TESTSAVE_Click()
If MsgBox("Changes have been made to this record." _
& vbCrLf & vbCrLf & "Do you want to save these changes?" _
, vbYesNo, "Changes Made") = vbYes Then
DoCmd.RunCommand acCmdSaveRecord
DoCmd.Close
DoCmd.OpenForm "NewATN2",,,,acFormAdd
Else
DoCmd.RunCommand acCmdUndo
End If
End Sub

Crosschecking account number between two access tables when using forms

I am using a form to enter new data into the table: tblMonthlySummary. I am entering Account Number, Currency, and Closing Local Balance.
When I enter the account number into table tblMonthlySummary, I would like Access, either through a macro or VBA, to crosscheck the table tblStaticBankDetails for the account number that would be added to tblMonthlySummary.
If the account number does not exist in tblStaticBankDetails, I would like VBA or a macro to return a Windows dialog box that says, "Invalid account number".
Here is the current code I have in VBA for my Add Entry to Table Button, Clear Fields, and Close Form. These buttons are on a form labeled: frmMonthlyManualUpdate.
Add Data Code
Private Sub cmdAdd_Click()
'add data to table
CurrentDb.Execute "INSERT INTO tblMonthlySummary([Account Number], [Currency], [Closing Balance]) " & _
" VALUES('" & Me.txtAccountNumber & "','" & Me.cboGender2 & "', '" & Me.txtClosingBalance & "')"
'clear form
cmdClear_Click
'refresh data in list on form
frmMonthlySummarySub.Form.Requery
'confirmation of end
MsgBox "Account number and balance successfully recorded."
End Sub
Me.cbogender2 is my currency blank.
Clear Fields Code
Private Sub cmdClear_Click()
Me.txtAccountNumber = ""
Me.cboGender2 = ""
Me.txtClosingBalance = ""
'focus on TxtAccountNumber
Me.txtAccountNumber.SetFocus
End Sub
Close Form Code
Private Sub cmdClose_Click()
DoCmd.Close
End Sub
Using a combo box is the way to go.
You can set it's properties to only allow values that are on it's list to be entered into the field. Users can either choose a value from it's list OR type the account manually. It won't let a value be entered that is not on the list.
How many accounts might have their summary row missing?
If it's only a small number you might want to consider:
Using SQL to insert the account numbers that are not in tblMonthlySummary that are in tblStaticBankDetails.
Then the user can see which ones are missing and find out what the values should be. (ie filter to form to view account with missing details)
It is strange that you are using a button to execute and insert statement. It is more common for an Access form to be based on an updatable record source (like you list) and for the user to simply click the "new record" button to insert a row.
PS. I would expect referential integrity to be set up between these two tables to stop any incorrect values being entered for account number as well.
Harvey

MS Access form - New record on open and after saving record

In relation to my question posted here:
MS Access Form where user name is selected from list
I have created a form used as an employee time sheet. Users double click this form and another form pops up where they select their user name from a combo box and the main time sheet form loads up specifically for their user name. They are able to add new records, and also view their previous submissions by scrolling through the "Previous" and "Next" records.
However, when a user opens up the form, their last record displays. Also, when a new record is saved, that new record continues to display unless a user clicks the "New (blank) record" button.
Can someone help me modify the VBA code in the form to allow for:
1) A blank record to automatically load when the form is opened up
2) A blank record to automatically load when a new record is saved/modified.
This is the pop up window - form that pops up when you select the main data entry form:
This is the code behind the pop up form (above):
Private Sub Form_Current()
If VBA.Strings.Len(txtUN & "") = 0 Then DoCmd.OpenForm "frm_UserName", acNormal, , , , acDialog
If VBA.Strings.Len(txtUsername & "") = 0 Then txtUsername = txtUN
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim ctl As Control
On Error GoTo Err_BeforeUpdate
If Me.Dirty Then
If MsgBox("Do you want to save?", vbYesNo + vbQuestion, _
"Save Record") = vbNo Then
Me.Undo
End If
End If
Exit_BeforeUpdate:
Exit Sub
Err_BeforeUpdate:
MsgBox Err.Number & " " & Err.Description
Resume Exit_BeforeUpdate
End Sub
This is the main data entry form [Specialist - Timesheet Entry]:
This is the code behind the [Specialist - Timesheet Entry] form:
Option Compare Database
Private Sub cboUserName_AfterUpdate()
Forms![Specialist - Timesheet Entry].txtUN = cbousername
Forms![Specialist - Timesheet Entry].Filter = "user_full_name = '" & cbousername & "'"
Forms![Specialist - Timesheet Entry].FilterOn = True
'Forms![Specialist - Timesheet Entry].Requery
'DoCmd.Close
End Sub
Private Sub Form_Unload(Cancel As Integer)
If (VBA.Strings.Len(cbousername & "") = 0) Then
MsgBox "You must supply a user name before proceeding.", , "ERROR: Missing Info."
Cancel = True
End If
End Sub
Once the [Specialist - Timesheet Entry] is selected, the [form_UserName] pops up. You must select a user name from the list, and hit the "X" button. Once the "X" button is selected than the data entry form [Specialist - Timesheet Entry] will appear for a user to enter the necessary fields. I am also looking for the user to not have to hit the "X" button in order to initiate the [Specialist - Timesheet Entry] form. Ideally once the user selects the user name from the drop down list in the pop up, i'd like the form to close automatically and generate the [Specialist - Timesheet Entry] form.
I think I have something for you here.
I share the same sentiment as OverMind - I'm iffy about the Me.Dirty code. I don't think it's necessary. All you need to do is click the dropdown, select a name and open a form that is filtered using the selected name. Same thing goes for the Form_BeforeUpdate event. You're not actually modifying any data, you're just clicking and selecting. I recommend removing both of those events.
If you don't mind, I reworked this a little since I don't have the entire application in front of me. Try the following (there's a few parts):
1.) frm_UserName
Comment your existing code in the cboUserName_AfterUpdate event and add this line:
DoCmd.OpenForm "Specialist - Timesheet Entry"
'Open the main timesheet form and set the filter based on the dropdown selection in the previous form.
Forms![Specialist - Timesheet Entry].Filter = "Username = '" & Forms![frm_UserName].cboUserName.Column(1) & "'"
'Turn the filter on.
Forms![Specialist - Timesheet Entry].FilterOn = True
'Set the username textbox to the selected record.
Forms![Specialist - Timesheet Entry].txtUN = Me.cboUserName.Column(1)
'Jump to a new record even though the form is filtered.
Forms![Specialist - Timesheet Entry].SetFocus
DoCmd.GoToRecord , , acNewRec
'Close the previous form - we no longer need it.
DoCmd.Close acForm, "frm_UserName"
This will open the Timesheet - Specialist Entry from frm_UserName form "automatically load [a blank record] when the Timesheet form is opened up" using a the selection as the filter.
2.) Specialist - Timesheet Entry
Create a new procedure in the Form Open event and add this code:
Private Sub Form_Open(Cancel As Integer)
DoCmd.OpenForm "frm_UserName", acNormal, , , , acDialog
End Sub
Next, create a new procedure in the Save button Click event and add this code:
Private Sub cmdSave_Click()
'Save the record.
RunCommand acCmdSaveRecord
'Load a new record after the save.
DoCmd.GoToRecord , , acNewRec
End Sub
This will automatically load a blank record when a new record is saved/modified.
I do hope this helps. If you have any questions at all, please let me know. I'd be glad to help even further. I also have a working copy available...