How to show query results in an Access form? - vba

The form contains a textbox, a button and a subform (which I created by just dragging the query into the form). The table contains 8 fields. When any of those field keywords are typed in, all records matching those keywords should display in the same form. But instead of that, Access shows the results in a separate window. How can I fix this?
I haven't included the complete where clause to this, here's my query:
SELECT *
FROM table
WHERE Account_Name Like "" & [Forms]![Form1]![Text0] & "" OR Opportunity_Name Like "" & [Forms]![Form1]![Text0] & "" OR .....
And I have assigned this query to the button. Here's my on click event of the button:
Private Sub Command2_Click()
On Error GoTo Err_Command2_Click
Dim stDocName As String
stDocName = "Search2"
DoCmd.OpenQuery stDocName, acNormal, acEdit
DoCmd.Requery
subform.Requery
Exit_Command2_Click:
Exit Sub
Err_Command2_Click:
MsgBox Err.Description
Resume Exit_Command2_Click
End Sub
It does the work but the only problem is that it shows data in a separate window. How can I fix this? Thanks.

It opens a separate window because that is what the code is written to do. Remove both DoCmd lines. Then code to requery must reference subform container control.
Me.subformContainerName.Requery

Related

Insert Document Property Object

In my Word (Office 365) document, I can insert a Quick Part for the company name by clicking Insert / Quick Parts / Document Property / Company.
I'd like a macro to do that, so I can pop a button on my Quick Access toolbar to make it one click not four.
When I record the process, the macro does not register the insert. I found that the following VBA code inserts the current text of the field, but not the content control itself:
ActiveDocument.Content.InsertAfter
ActiveDocument.BuiltInDocumentProperties(wdPropertyCompany)
I figure there must a single line of VBA that would insert the Company Quick Part field into my document, as if I had done those four clicks.
The trick is to map the content control to the Company
Sub insertCompanyCC()
On Error GoTo err_insert
Dim cc As ContentControl
Set cc = ActiveDocument.ContentControls.Add(wdContentControlText, Selection)
With cc
.Title = "Company"
.XMLMapping.SetMapping "/ns0:Properties[1]/ns0:Company[1]"
End With
exit_insert:
Exit Sub
err_insert:
Select Case Err
Case 4605
MsgBox "Please move your cursor outside of the content control.", vbExclamation
Case Else
Err.Raise Err.Number, Err.Source
End Select
Resume exit_insert
End Sub
e.g. /ns1:coreProperties[1]/ns0:creator[1] would insert the author.

Access VBA, Unable to close the current form. Error 2585 This action can't be carried out while processing a form or report event

This should be so simple BUT!
My opening form displays a dropdown to select user name (filled from a select query) and an unbound text box to enter password. The password is compared with a fixed string and if it matches I need to close this form and open the main menu. There is also a button to close the whole system if you don't know the password.
The tab order is dropdown, text box, exit button.
After many varients of AfterUpdate, BeforeUpdate,lostFocus I put this code in the Got Focus event of the exit button, so that exit from the password box will trigger it. Each function sets the focus back to an appropriate incorrect entry but if all is correct the program flows to the last two lines.
Private Sub btnExitAll_GotFocus()
If PasswordNotOk() Then Exit Sub
If UserNotOK() Then Exit Sub
DoCmd.OpenForm "Main Menu"
DoCmd.Close acForm, "Opening screen", acSaveNo
End sub
The "Close" line fails with 2585. I have tried reversing the order of the last two lines.
I have even put the last two line in the click event of an temporary button, where they work exactly as intended! So I tried calling that button's click event from this sub, but get the same error.
I have tried Unload me but that throws error 361 Can't load or unload this objectbut I just can't get this form to close itself.
If it helps, when I tried to do this in the before update event of the UNBOUD text box I got errors saying I couldn't do this before I saved the data.
Please someone what am I doing wrong?
I am using Me.Name to close a form from itself. You might need to clear changes before you close.
Private Sub btnExitAll_Click()
If PasswordNotOk() Then Exit Sub
If UserNotOK() Then Exit Sub
DoCmd.OpenForm "Main Menu"
' Discard changes so we can close without saving
If Me.Dirty = True Then
Me.Undo
End If
DoCmd.Close acForm, Me.Name, acSaveNo
End Sub

Access VBA code to go to a specific record on another form via selecting it in a list drop down combo box

I have a form with a combo box with a list of jobs located on a different form all with their own unique record. I would like to be able to click the drop-down of the combo box, select a specific job and then have it open the specific job record I select... Struggling with coming up a VBA code that will do this. Can anyone help? Thanks
If you want to open another form that just shows the record selected in a combo box, then you can use the control's AfterUpdate event, and use the "Where condition" argument of the OpenForm action:
Private Sub cboSearch_AfterUpdate()
On Error GoTo E_Handle
DoCmd.OpenForm "frmData", , , "FileID=" & Me!cboSearch
sExit:
On Error Resume Next
Exit Sub
E_Handle:
MsgBox Err.Description & vbCrLf & vbCrLf & "frmSearch!cboSearch_AfterUpdate", vbOKOnly + vbCritical, "Error: " & Err.Number
Resume sExit
End Sub
Regards,

Odd problem - Form won't close when opening new form using WHERE

I have two forms (in question here). frmContactList and frmContactDetails. frmContactList is a datasheet list of last names, first names, and e-mail addresses. The idea (which works and hasn't been a problem so far) is that when you double-click either the last name or the first name, the form frmContactDetails is opened to the specific record chosen on frmContactList. All this works just fine. Where I'm having the trouble (annoyingly) is I want it to OPEN frmContactDetails then CLOSE frmContactList. OPEN works fine, close doesn't happen until I close frmContactDetails, though.
I BELIEVE the error is with the fact that I started with a "prefab" Access template and went to editing it from there. I didn't create this project from scratch. Won't make that mistake again. Thought I'd save time. Yeah right...
Here's the DblClick() coding I'm using for the Last Name (First Name will be the same once I figure out the bug:NOTE: I commented out the Form.Dirty and Macro Error code because it's part of that messy "prefab" Access stuff.
Private Sub Last_Name_DblClick(Cancel As Integer)
On Error GoTo Last_Name_DblClick_Err
On Error Resume Next
' If (Form.Dirty) Then
' DoCmd.RunCommand acCmdSaveRecord
' End If
' If (MacroError.Number <> 0) Then
' Beep
' MsgBox MacroError.Description, vbOKOnly, ""
' Exit Sub
' End If
DoCmd.OpenForm "frmContactDetails", acNormal, "", "[ID]=" & ID, , acDialog
DoCmd.Close acForm, "frmContactList"
Last_Name_DblClick_Exit:
Exit Sub
Last_Name_DblClick_Err:
MsgBox Error$
Resume Last_Name_DblClick_Exit
End Sub
Here are a few pictures of the design.
Simple design.
Here frmContactDetails is opened (personal information is blacked out) showing frmContactList in the background not closed.
When you open a form with WindowMode:=acDialog, then the code stops at this code line until the opened form is made invisible or closed. Just drop this parameter.
DoCmd.OpenForm "frmContactDetails", View:=acNormal, WhereCondition:="[ID]=" & ID
DoCmd.Close acForm, Me.Name
Note: Use WindowMode:=acDialog if you need the data entered in a dialog form at the call site. In this case, don't close the dialog form with Me.Close but instead hide it with Me.Visible = False, then get its data through Forms!fdlgMyDialogForm!TheData.Value and finally close it at the call site with DoCmd.Close acForm, "fdlgMyDialogForm".
Closing the current form with Me.Name is more robust than specifying the name as string constant.

Access VBA SendKeys alternative

I just inherited a personnel database at work that has not been touched in a few years and they want me to make it functional. I've been able to update most of the things but I've run into an issue with a macro that was input that uses the SendKeys action:
Private Sub Command3_Click()
On Error GoTo Err_Command3_Click
DoCmd.OpenTable "tblEmployees", acNormal, acEdit
DoCmd.GoToControl [employee]
DoCmd.FindRecord Forms![EmployeeRemoval]![cboEmpSelect],
DoCmd.GoToControl [pastemployee]
SendKeys "1~", True
DoCmd.Close acTable, "tblEmployees"
Exit_Command3_Click:
Exit Sub
Err_Command3_Click:
MsgBox Err.Description
Resume Exit_Command3_Click
At this point with Access 2010 it just sends the program into a loop opening and closing the table while also turning Num Lock on and off. I figured the best replacement would be an Update statement, but am not sure what the Where criteria would be to update the single cell that needs updating (in this case only the PastEmployee cell for the one employee in question at any given time would need to update to True instead of False). In the macro it was referencing (through the FindRecord command) a ComboBox in which the employee is selected that queried Last Name, First Name, and employee number from the employee table.
db.Execute Update tblEmployees Set [PastEmployee] = 1 Where XXXXXXXX
Any help in either a way to make the SendKeys command just work or to get the update statement working would be helpful.
The posted code is not a macro, it is VBA. Macros in Access are very different.
Users should not interact with tables and queries, just forms and reports.
In Access Yes/No field, 0 is False and -1 is True.
Consider:
Private Sub Command3_Click()
On Error GoTo Err_Command3_Click
CurrentDb.Execute "UPDATE tblEmployees SET PastEmployee=True WHERE employee=" & Me.cboEmpSelect
Exit_Command3_Click:
Exit Sub
Err_Command3_Click:
MsgBox Err.Description
Resume Exit_Command3_Click
End Sub
If the form were bound to tblEmployees and focus on the record to edit, instead of the UPDATE action, simply: Me!PastEmployee = True. Although, if the record is viewed on form, user just needs to click checkbox bound to PastEmployee and no code would be needed.