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.
Related
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.
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
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.
I'm using several excel files and each one is making some calculations using VBA. Now I'm working on one file which will run each file one after another and calculate them all with one click. Unfortunately at the end of calculation for each file there is Msgbox "Report updated" and my macro stops and is waiting to confirm "OK" manually before it will close first file and open next one.
I cannot remove this lines with msgboxes because some people will use just one file and they need to be informed that everything is finished.
And here is my question: How can I close this msgbox automatically?
Thanks in advance
I think it is impossible, because MsgBox is modal. But why your macro displays it at first? You may add conditional instruction that checks if the sheet on which it works is displayed. Something like:
If ActiveSheet.Name = wsProcessedSheet.Name Then MsgBox "Report updated"
So, the message will be displayed for normal user and not for your supermacro. Alternatively, you could disable the MsgBox only for your machine:
If Environ("username")<> "your user name" Then MsgBox "Report updated"
Thank you for interesting ideas.
I've just figured out different solution. Originally I had button which run macro Update()
Sub Update()
...calculations....
Msgbox "Report updated"
End Sub
Now I removed msgbox from this macro and created two macroslike below:
Sub ButtonClick()
Call Update
Msgbox "Report updated"
End Sub
Sub Update()
...calculations....
End Sub
And now button is running macro ButtonClick and in external file I can use macro Update() which didn't contain msgbox.
But I still wondering if there is any way to confirm msgbox alerts
I'm creating a task list style excel sheet in which people are assigned tasks and can use the sheet as a progress tracker.
Once a task is completed, a person marks it off as completed and the sheet pops up with a message box asking whether a change reference number is needed.
However, when I filter by say, the person who completed the task, and try to add a new row for a new task I get the Run Time Error 13 Type Mismatch. I think it's because my VBA is constantly checking for the status of the task to be completed, so when you create a blank row, there's nothing in the status column.
Here's the below code that's highlighted with the error message.
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim answer
If Not Intersect(Target, Range("H5:H1000")) Is Nothing Then
If (Target.Value) = "Completed" Then
answer = MsgBox("Do you need to add a change reference?", vbYesNo + vbQuestion, "Change Reference Reminder")
If answer = vbYes Then
MsgBox "Add in the column to the right", , ("Change Reference Tip")
Else
'do nothing'
End If
End If
End If
End Sub
There are a few things going on here; too many to describe in a comment.
First of all, the Worksheet_Change sheet macro is triggered by an event, specifically when one or more cells' contents change. If you want to avoid heavy processing when you insert a row (subsequently changing the values of many cells), you need it to kick out if Target is more than a single cell. Typically, including If Target.cells.count > 1 then exit sub as the first line is sufficient to accomplish this. This is especially important as you want to compare a single changed cell's value to Completed.
Your code as supplied doesn't actually change anything but it looks like at some point it will be intended to. In order that changing a cell's value does not launch a second iteration of the same Worksheet_Change event maco (subsequently trying to run on top of itself) you would halt event handling temporarily and restart it just before leaving the macro.
If you turn off events, you need error handling so that they are turned back on in case of an error. Errors can occur to the best written code procedure and you want to catch and report the error to somewhere like the Immediate window then turn the event handling back on before safely exiting the event macro.
Minor point but I never Dim a variable until my boolean conditions dictate that I am actually going to use it.
With these 3½ things in mind, the sample code provided might be better as,
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Cells.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("H5:H1000")) Is Nothing Then
On Error GoTo Erreur
Application.EnableEvents = False
If LCase(Target.Value) = "completed" Then
Dim answer As Variant
answer = MsgBox("Do you need to add a change reference?", vbYesNo + vbQuestion, "Change Reference Reminder")
If answer = vbYes Then
MsgBox "Change references are added in the column to the right." _
& Chr(10) & Chr(10) & "Let's go there now.", vbOKOnly, "Change Re ference Tip"
Target.Offset(0, 1).Activate
Else
'do nothing'
End If
End If
End If
GoTo Fìn
Erreur:
Debug.Print Err
Fìn:
Application.EnableEvents = True
End Sub
I've added one line of code that moved the user to the cell one column to the right of the cell that triggered the event.