Access modal form updates database then calling form update does not - vba

In Access a header form needs an address associated with it. The header form calls a modal address form. On Save the modal address form will write the address ID to the header form's record source address ID then close. Coming back to the calling header form should then list the related address ID after a requery, that is the FK link. I am not getting the address form's address ID written to the header form's record source address ID field and thus not listing on requery. A fair amount of this code is based on several SO questions.
The header form calls a wrapper function to manage the modal address form:
Private Sub txtJob_AfterUpdate()
gbolDropShipAddressCompleted = False
'Use global variables for use in frmDropShipAddress SQL update statement
gtxtCurrentJobNumber = Me.Job
gtxtCurrentJobRelease = Me.REL
gtxtCurrentJob = Me.txtJob
'Call the wrapper function
gbolDropShipAddressCompleted = fNewDropShipAddressCompletion()
If gbolDropShipAddressCompleted = True Then
'Me.Dirty = False
Me.Requery
With Me.RecordsetClone
.FindFirst "[Job] = '" & gtxtCurrentJobNumber & "' And [REL] = '" & gtxtCurrentJobRelease & "'"
If Not .NoMatch Then
If Me.Dirty Then
Me.Dirty = False
End If
Me.Bookmark = .Bookmark
End If
End With
'Reset all variables
gbolDropShipAddressCompleted = False
gtxtCurrentJobNumber = ""
gtxtCurrentJobRelease = ""
gtxtCurrentJob = ""
End If
End Sub
The wrapper function:
Public Function fNewDropShipAddressCompletion() As Boolean
'Manages call to frmDropShipAddress
DoCmd.OpenForm "frmDropShipAddress", , , , , acDialog, "From frmPackingSlipHeader"
fNewDropShipAddressCompletion = gbolfrmDropShipAddressTofrmPackingSlipHeader
DoCmd.Close acForm, "frmDropShipAddress"
End Function
The modal form's two events:
Private Sub btnSaveAddressInfo_Click()
DoCmd.RefreshRecord
'Update MAIN table with new address id using global variables
CurrentDb.Execute " UPDATE [MAIN] " & _
" SET intADDRESS_ID = " & Me.txtAddress_ID & _
" WHERE JOB = '" & gtxtCurrentJobNumber & "'" & _
" AND REL = '" & gtxtCurrentJobRelease & "'"
gbolfrmDropShipAddressTofrmPackingSlipHeader = True
Me.Visible = False
End Sub
Private Sub Form_Open(Cancel As Integer)
gbolfrmDropShipAddressTofrmPackingSlipHeader = 0
If Me.OpenArgs = "From frmPackingSlipHeader" Then
DoCmd.GoToRecord , , acNewRec
'Coded because TabCtlDatePicker opens all forms and openargs is NULL and causes all forms to be affected
ElseIf IsNull(Me.OpenArgs) Then
Exit Sub
' Else
' Me.PopUp = True
' Me.Modal = True
' Me.BorderStyle = 1
' Me.NavigationButtons = False
' Me.RecordSelectors = False
End If
End Sub
The Access front-end uses several tab controls to select between many different reports/date and or data pickers all sorted into tab control pages by topic. The back-end is MS SQL server via linked tables. The address form may be opened for generic viewing or called (modal). I can't seem to get the correct VBA to get the address form's address ID written to the header's record source and the VBA to get the header to requery and return to the correct record. Any help would be appreciated and TIA.
Tim

Related

Close subform, remove main form filter and go back to original record

I am opening a subform from my main form to allow data to be changed. Once the changes are made, I want to pass the data back to the main form. remove the filter, and go back to the original record. I have the Primary Key in the subform so I am passing it back. I used some code from another user's but it did not work not is my code. Any thoughts?
Private Sub cmd_close_Click()
Dim result As String
Dim ID As Variant
result = MsgBox("Save Geo Location?", vbOKCancel, "Save Geo Location")
If result = vbOK Then
Forms!frm_acct_select!GeoLoc_X = Me.txt_GeoLocX
Forms!frm_acct_select!GeoLoc_Y = Me.txt_GeoLocY
Forms!frm_acct_select.FilterOn = False
'this code fails immediately
With frm_acct_select.Form
ID = Me.txt_ParentID.Value
.FilterOn = False
.Recordset.FindFirst "ParentAccountID=" & ID
End With
'this code fails type mismatch criteria at the recordset.findfirst line
' With Forms!frm_acct_select
' ID = Me.txt_ParentID.Value
' .FilterOn = False
' .Recordset.FindFirst "ParentAccountID = " & ID
' End With
DoCmd.Close acForm, "sfrm_geoloc_update", acSaveNo
Else
DoCmd.Close acForm, "sfrm_acct_select_search", acSaveNo
End If
End Sub
Apparently, I still need to work on my string formatting.
Declaring ID as String was correct however I needed to add quotes to my code.
.Recordset.FindFirst "ParentAccountID = '" & ID & "'"

User Level permission to different forms

I have code that works for opening one particular form, but I need to setup different user levels which can then be used to restrict access to different forms in the Access program.
I have a login screen which uses data (username/password) from a table employee, this table also has a foreign key column called employeeTypeId, additionally I have a table called Access which has different employeetypeId as 1,2,3,4,5.
5 is supposed to be admin and 1 is read-only user and so on. The third table is EmployeeAccess , it has employeeTypeId as well as column HasAccess with yes/no datatype.
I am using this to write a code in VBA to ensure only certain users have access to certain forms.
I have a code that works for one form, I am trying to figure out how to use this HasAccess column in conjunction with employeeTypeId and different form-names (need to figure out how to use that) to ensure employeetypeId=5 users have access to all forms, employeetypeId=4 has access to lets say all forms except employee table form and employeetypeId=3 has access to edit only a select few forms and so on.
This is the code for accessing the form after login (on load):
Private Sub Form_Load()
If DLookup("HasAccess", "EmployeeAccess", "EmployeeTypeId=" & TempVars("EmployeeType") & " AND FormName='" & Me.Name & "'" = False) Then
MsgBox "Yo do not have access"
DoCmd.Close acForm, Me.Name
End If
End Sub
This is the code in the login form:
Private Sub btnLogin_Click()
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("Employee", dbOpenSnapshot, dbReadOnly)
rs.FindFirst "UserName='" & Me.TextUserName & "'"
If rs.NoMatch = True Then
Me.LabelWrongUser.Visible = True
Me.TextUserName.SetFocus
Exit Sub
End If
Me.LabelWrongUser.Visible = False
If rs!Password <> Encrypt(Me.TextPassword) Then
Me.LabelWrongPass.Visible = True
Me.TextPassword.SetFocus
Exit Sub
End If
Me.LabelWrongPass.Visible = False
TempVars("EmployeeType") = rs!EmployeeTypeId.Value
End Sub
Currently the code opens all the forms when login is correct, I want to change that and give access based on employeeTypeId.
==========================
Private Sub btnLogin_Click()
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("Employee", dbOpenSnapshot, dbReadOnly)
rs.FindFirst "UserName='" & Me.TextUserName & "'"
If rs.NoMatch = True Then
Me.LabelWrongUser.Visible = True
Me.TextUserName.SetFocus
Exit Sub
End If
Me.LabelWrongUser.Visible = False
If rs!Password <> Encrypt(Me.TextPassword) Then
Me.LabelWrongPass.Visible = True
Me.TextPassword.SetFocus
Exit Sub
End If
Me.LabelWrongPass.Visible = False
TempVars("EmployeeType") = rs!EmployeeTypeId.Value
If DLookup("HasAccess", "EmployeeAccess", "EmployeeTypeId=" & TempVars("EmployeeType")) Then
TempVars("FormName") = rs!FormName.Value And DoCmd.OpenForm (
I just want to know how to use the DoCmd.OpenForm with the TempVars("FormName") and if it is possible.
Using the below code I was able to achieve the result:
The Goal was to filter based on EmployeeTypeId and HasAccess set as True (A combination of both to open forms)
TempVars("FormName") = DLookup("FormName", "EmployeeAccess", "EmployeeTypeId=" & TempVars("EmployeeType") & " And HasAccess = " & True & " ")
DoCmd.OpenForm TempVars("FormName")

Filtering data on form causes combo box to not function

I have a form that displays student records.
I have a combobox that allows you to select a student.
Finally, I have two buttons for filtering. One filters on all Students the other only active Students.
Two Issues which I beleive are related.
One, when the form loads the cbo does NOT work.
Two, if I select the "All Students" button the cbo works. When I select the "Active Students" button the cbo does NOT work again.
Form Record Source:
Select tblStudents.*
Form Load:
Private Sub cmdStudent_Click()
On Error GoTo cmdStudent_Click_Err
DoCmd.OpenForm "frmStudents", acNormal, "", "[Last3]=" & "'" & "Sanders
862" & "'", , acNormal
cmdStudent_Click_Exit:
Exit Sub
cmdStudent_Click_Err:
MsgBox Error$
Resume cmdStudent_Click_Exit
End Sub
ComboBox Code:
Private Sub cboFindRecord_AfterUpdate()
' Find the record that matches the control.
Dim rs As Recordset
Set rs = Me.RecordsetClone
rs.FindFirst "[SN] = '" & Me![cboFindRecord] & "'"
If Not rs.EOF Then Me.Bookmark = rs.Bookmark
End Sub
All Students Button:
Private Sub cmdAllStudents_Click()
cmdAllStudents.ForeColor = 16777215
cmdAllStudents.BackColor = 16711680
cmdAllStudents.FontBold = True
cmdActiveStudents.ForeColor = 0
cmdActiveStudents.BackColor = 16777215
cmdActiveStudents.FontBold = False
Me.FilterOn = False
cboFindRecord.RowSourceType = "Table/Query"
cboFindRecord.RowSource = "SELECT tblStudents.SN, tblStudents.Last3 FROM
tblStudents ORDER BY tblStudents.Last3;"
cboFindRecord.Requery
Call cboFindRecord_AfterUpdate
cboFindRecord.SetFocus
End Sub
Active Student Button:
Private Sub cmdActiveStudents_Click()
cmdActiveStudents.ForeColor = 16777215
cmdActiveStudents.BackColor = 16711680
cmdActiveStudents.FontBold = True
cmdAllStudents.ForeColor = 0
cmdAllStudents.BackColor = 16777215
cmdAllStudents.FontBold = False
Me.FilterOn = True
cboFindRecord.RowSourceType = "Table/Query"
cboFindRecord.RowSource = "SELECT tblStudents.SN, tblStudents.Last3,
tblStudents.Status_ID FROM tblStudents WHERE (((tblStudents.Status_ID) Not
In (6,7))) ORDER BY tblStudents.Last3;"
cboFindRecord.Requery
Call cboFindRecord_AfterUpdate
cboFindRecord.SetFocus
End Sub
Thank you for any assistance.
I have tried modifying the record set to match all or active students with no change.
OK, not sure why this is happening but I have fixed my issue. The problem is with the form loading code.
When I was loading the Student from from another form, I had it load our test student by default. My theory is that this was changing the record-set. I would appreciate an explanation if someone knows.
I removed forcing the student record when loading the student form and put the code in the On Load event for the form.

MS Access VBA to enable / disable a text box

I have the below VBA code in access to enable/disable a text box.
When the code is executed the tonnes textbox remains disabled.
Am I missing an additional property?
Private Sub EnableTonnes()
Dim sCode As String
sCode ="xx"
' set default values for tonnes enabled and locked properties
Tonnes.enabled = False
Tonnes.Locked = True
If sCode = "xx" Then
' enable tonnes field
Tonnes.enabled = True
Tonnes.Locked = False
End If
End Sub
Your code looks ok. TextBox properties are set in form design mode and can only be permanently changed in form design mode. You do some sophisticated coding to open the form in design mode, change the properties and then save the form... or do it manually. You will always be able to use your code to control those properties at runtime.
Option Compare Database
Option Explicit
Private Sub cmdGo_Click()
Dim sCode As String
sCode = "xx"
' set default values for tonnes enabled and locked properties
txtTonnes.Enabled = False
txtTonnes.Locked = True
If sCode = "xx" Then
' enable tonnes field
txtTonnes.Enabled = True
txtTonnes.Locked = False
End If
MsgBox txtTonnes.Name & " Enabled status is " & txtTonnes.Enabled
MsgBox txtTonnes.Name & " Locked status is " & txtTonnes.Locked
End Sub
Private Sub Form_Load()
MsgBox txtTonnes.Name & " Enabled status is " & txtTonnes.Enabled
MsgBox txtTonnes.Name & " Locked status is " & txtTonnes.Locked
End Sub

#Name? on form after requery in Access 2010

I am using VBA and SQL to re-query my main form based on criteria entered in several controls on a pop up form. As far as I can tell the code is running correctly, the database is re-queried based on the criteria I enter, but 2 of my controls on my main form show as #Name? or blank after re-querying based on the criteria. Anyone know how I can fix this???
The code that runs the re-query is:
Public Sub SuperFilter()
On Error GoTo Err_AdvancedFilter_Click
Dim strSQL As String
Dim strCallNumber As String
Dim strAsgnTech As String
Dim strClientID As String
Dim strCallGroup As String
Dim strPriority As String
Dim strOpenStatus As String
If IsNull(Forms![frmTips&Tricks].txtCallNumber) = False Then
strCallNumber = " (((CallInfo.CallNumber) = forms![frmTips&Tricks].[txtCallNumber])) and "
Else
strCallNumber = ""
End If
If IsNull(Forms![frmTips&Tricks].cboAsgnTech) = False Then
strAsgnTech = " (((CallInfo.AsgnTech) = forms![frmTips&Tricks].[cboasgntech])) and "
Else
strAsgnTech = ""
End If
If IsNull(Forms![frmTips&Tricks].cboClientID) = False Then
strClientID = " (((CallInfo.ClientID) = forms![frmTips&Tricks].[cboClientID])) and "
Else
strClientID = ""
End If
If IsNull(Forms![frmTips&Tricks].cboCallGroup) = False Then
strCallGroup = " (((CallInfo.AsgnGroup) = forms![frmTips&Tricks].[cboCallGroup])) and "
Else
strCallGroup = ""
End If
If IsNull(Forms![frmTips&Tricks].cboPriority) = False Then
strPriority = " (((CallInfo.Severity) = forms![frmTips&Tricks].[cboPriority])) and "
Else
strPriority = ""
End If
If Forms![frmTips&Tricks].optOpenStatus.Value = 1 Then
strOpenStatus = " (((CallInfo.OpenStatus) = True))"
Else
strOpenStatus = " (((CallInfo.OpenStatus) is not null ))"
End If
strSQL = "SELECT CallInfo.CallNumber, CallInfo.ClientID,* " & _
"FROM dbo_HDTechs INNER JOIN ([User] INNER JOIN CallInfo ON User.ClientID = CallInfo.ClientID) ON dbo_HDTechs.TechName = CallInfo.AsgnTech " & _
"WHERE " & strCallNumber & strAsgnTech & strClientID & strCallGroup & strPriority & strOpenStatus & _
"ORDER BY CallInfo.RcvdDate;"
Form.RecordSource = strSQL
Me.cboCallNumber.RowSource = strSQL
Form.Requery
If Me.RecordsetClone.RecordCount = 0 Then
MsgBox "No Records Found: Try Diferent Criteria."
Form.RecordSource = "qryservicerequestentry"
Me.cboCallNumber.RowSource = "qryservicerequestentry"
Exit Sub
End If
Me.cmdSuperFilterOff.Visible = True
Exit Sub
Exit_cmdAdvancedFilter_Click:
Exit Sub
Err_AdvancedFilter_Click:
MsgBox Err.Description
Resume Exit_cmdAdvancedFilter_Click
End Sub
The first control in question is a combo box that displays the Client Name from the CallInfo form (Main Form).
Control Source: ClientID
And when expanded lists all available clients to select from the Users form (User ID is linked between the User form and CallInfo form).
Row Source: SELECT User.ClientID FROM [User];
After the re-query, this combobox will be blank, sometimes showing #Name? if you click on it.
The second control in question is a text box that shows the Client's phone number.
Control Source: PhoneNo
After the Re-query, this text box always displays #Name?
The third control in question is a text box that displays the clients office location.
Control Source: Location
What really baffles me is that THIS text box displays correctly after the re-query. I don't know why it would display the correct data when the Phone Number text box does not, seeing as they are so similar and work with similar data....
To Compare, the The form record source is normally based on:
SELECT CallInfo.CallNumber, CallInfo.ClientID, CallInfo.RcvdTech, CallInfo.RcvdDate, CallInfo.CloseDate, CallInfo.Classroom, CallInfo.Problem, CallInfo.CurrentStatus, CallInfo.Resolution, CallInfo.Severity, CallInfo.OpenStatus, CallInfo.AsgnTech, dbo_HDTechs.Email, CallInfo.FullName, CallInfo.AsgnGroup, User.Location, User.PhoneNo, CallInfo.OpenStatus
FROM dbo_HDTechs INNER JOIN ([User] INNER JOIN CallInfo ON User.ClientID = CallInfo.ClientID) ON dbo_HDTechs.TechName = CallInfo.AsgnTech
WHERE (((CallInfo.OpenStatus)=True))
ORDER BY CallInfo.RcvdDate;
Just going on what you wrote, I may take a slightly different approach (just personal preference).
I would change all of your 'IsNull' tests to also check for 'Empty'. i.e.
If IsNull(Forms![frmTips&Tricks].cboClientID) = False AND ...cliientID <> ""
Just today I had an issue relating to form references in a query WHERE clause, so I changed to:
strClientID = " (((CallInfo.ClientID) = '" & forms![frmTips&Tricks].[cboClientID] & "')) and"
Add a Debug.Print of your generated SQL, then look at it and try to run that SQL manually
Good Luck,
Wayne
Solved by designating the form in the control source like: CallInfo.ClientID
I still don't know why the Client Office displayed Correctly... Anybody have a hint? :)
TE