Crosschecking account number between two access tables when using forms - 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

Related

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

Update Access Tables with Data entered in forms

I have created a form to search for a record based on user input. user searches for a Site using SiteID which he enters and clicks okay. Which opens up another form with all the details for the site. User can then make changes to the Site details in this form and when he presses enter button it should update the table with all the changes made. Changes could be one text box or several text boxes.
I wrote a query but it is throwing error:
Private Sub Enter_Click()
SQL = "UPDATE Tracker " _
& " SET Tracker.[Site ID] = " & Me.[Site ID] _
& " , Tracker.[EHR Vendor] = " & Me.[EHR Vendor] _
& " , Tracker.[Site Name] = " & Me.[Site Name] _
& " WHERE Tracker.[Site ID] = " & Me.[Site ID] & ";"
DoCmd.RunSQL SQL
End Sub
Is this the best way to proceed or is there another method I can follow.
I would create a form, let’s call it subtracker, that contains all of the fields in the tracker table. Then in design view of the form where the user selects the siteID I add the subtracker form as a sub form and link the siteID field on the sub form to the selection box on the main form. Adding a sub form is an option on the design tab

Access, Opening the Same Form in Different ways using Buttons

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
}

Cannot view records in form ms access after assigning record-source

Please help,
I have this table ‘Source_RM’:
Source.........Raw Material.....Waste Date.....Waste Quantity
Store.............Sugar...........13/06/2013.................10
Store.............Salt...............12/06/2013.................13
Production....Sugar............17/06/2013.................10
Vendor..........Sugar............15/06/2013.................10
I have a main form where the client can perform queries passed on controls (text box, combo box ..)
In the main form I need to display a query result passed on combo box value:
The result wanted: the client choose the source from a combo box "cmbSource" so the result will be a table contain the source and total waste i.e.:
Source...Total
Store.......23
What I did is:
Created a form (“Blank Form”) name it: frmResult
In the main form I added Subform, In subform wizard in use an existing form I chose ‘frmResult’
After I choose the source from ‘cmbSource ‘ and click the button’ cmdFilter’
I wrote this code in VBA :
Private Sub cmdFilter_Click()
Me.FormTemp.Form.RecordSource = "SELECT Source_ID, SUM(Quantity) AS Total FROM Source_RM WHERE Source = ' " & Me. cmbSource.Value & " ' GROUP BY Source_ID;"
End Sub
After I click the button, the FormTemp has the RecordSource (I know from the record number at the bottom bar’ 1 of 1’) but the FormTemp still blank .. I need to show the result of query in FormTemp
Note : Date entry in FormTemp is set to NO.
I put the default view of FormTemp to Datasheet.
Please any Ideas
Thank you