How to close the MS Access auto dialer after 5 seconds - vba

When I use the MS Access Auto Dialer it opens the form, but a user must press the "Hang UP" button to close the form. How can I have the form close after a 5 second delay?
Sub CallCandidate(PhoneNumber As String, HangUpDelay As Integer)
On Error GoTo Err_CallCandidate
Application.Run "utility.wlib_AutoDial", PhoneNumber
Exit_CallCandidate:
Exit Sub
Err_CallCandidate:
MsgBox Err.Description
Resume Exit_CallCandidate
End Sub
I would like the passed integer "HangUpDelay" to be the delay time before it hangs up and closes the form; so, in essence, automatically executing the "Hang Up" button being pressed.

Related

Macro Freezes when ListBox is clicked

I'm using Office Professional Plus 2019. I have a ListBox that sometimes freezes the macro. The UserForm (AffDateSelector) has a ListBox (DateSelectionList) and a Button (Continue). The UserForm is called from the driving Sub with AffDateSelector.show. The code for the form is below:
Option Explicit
Private Sub Continue_Click()
Dim lngCurrItem As Long
'Assign the selected value to the public variable strClassDate.
For lngCurrItem = 0 To DateSelectionList.ListCount - 1
If DateSelectionList.Selected(lngCurrItem) = True Then
strClassDate = DateSelectionList.List(lngCurrItem)
End If
Next lngCurrItem
'Unload the form and return to the calling Sub.
Unload Me
End Sub
Private Sub DateSelectionList_Click()
End Sub
Private Sub UserForm_Click()
End Sub
Private Sub UserForm_initialize()
'Load ListBoxAccounts with the public variable strDateArray
With DateSelectionList
.List = strDateArray
End With
'Default the first row in DateSelectionList to be selected.
DateSelectionList.Selected(0) = True
End Sub
I've been using F8 to step through the code. When the form is shown, the UserForm_initialize() sub is executed and the data display properly. If the user does nothing else except click the Continue button, the Continue_Click() sub gets executed, the variable strClassDate is populated, control is returned to the calling sub and all is well.
The problem comes when the user selects an item other than the one defaulted in the list. Once the user clicks on the ListForm, the sub DateSelectionList_Click() is executed, but there's nothing in that sub, so the macro freezes. If I put in "dummy code" into DateSelectionlist_Click() (e.g. strClassDate = strClassDate) that line is executed and then the macro freezes when the End Sub statement is executed.
How can I allow the user to click on a non-defaulted item in the list, keep the form displayed, and wait for the Continue button to be pressed?
The code was fine. I had a lot of windows open and the pop-up didn't appear I was running the code from the VB editor but the pop-up never appeared. When I pressed Alt + Tab it didn't appear in that list either.

VBA Access 2013 form: how to refresh?

I have a form Overview with a table that gets its data from a database. I would like to add a line to that database and update my table in Overview. I created a button in Overview which opens another form InputForm (that pops up in front of Overview). In VBA, I wrote some code that adds the data to the database using a query and then closes InputForm. When InputForm closes, Overview becomes visible again. Now, I would like to see the table in Overview updated with the new data. The problem is that it only updates when I click another button in Overview or when I close and reopen the form.
I tried the GotFocus, Activate, Click... events to Refresh the form but I guess that the form does not lose focus or is not deactivated when InputForm is opened so these events of course never happen. I also tried to Refresh Overview from within the InputForm. I also tried to find the Activate or Deactivate functions but they appear nowhere. I honestly don't know how to update the table in the Overview form.
EDIT:
Overview
Private Sub btnOpenInputForm_Click()
DoCmd.OpenForm FormName:="InputForm", OpenArgs:=0 & "," & 0
End Sub
InputForm
Private Sub btnAddRecord_Click()
'Read data from the input fields
'CurrentDb.Execute "INSERT..."
DoCmd.Close
End Sub
If I understand correctly, then your form named Overview is bound to the same table which will be a record added to by a button which also is on that form.
So the code to add the new record runs in the Click event of that button, right?
So then you would have to call Me.Requery after adding the record (The Me keyword is a reference to the form in that case).
Edit:
Regarding your new information I added this, showing how to call your InputForm as a modal dialog and read out the values in case the user didn't cancel it.
This would be your calling procedure:
Private Sub btnOpenInputForm_Click()
Const INPUT_FORM_NAME As String = "InputForm"
'Call the form as a modal dialog
DoCmd.OpenForm FormName:=INPUT_FORM_NAME, OpenArgs:=0 & "," & 0, WindowMode:=acDialog
'If the user cancelled the dialog, it is not loaded any more.
'So exit the sub (or maybe do some other stuff)
If Not CurrentProject.AllForms(INPUT_FORM_NAME).IsLoaded Then
Debug.Print "The user cancelled the dialog"
Exit Sub
End If
'You can now check a value in the dialog
If IsNull(Forms(INPUT_FORM_NAME).MyTextBox.Value) Then
Debug.Print "Null in the control"
Exit Sub
End If
'Or read a value into a variable
Dim myVariable As String
myVariable = Forms(INPUT_FORM_NAME).MyTextBox.Value
'Close the dialog form now
DoCmd.Close A_FORM, INPUT_FORM_NAME
'Build and execute your sql here, like you already did in the dialog form before
'CurrentDb.Execute "INSERT..."
Debug.Print "User entered: ", myVariable
'And finally requery the form
Me.Requery
End Sub
In the dialog form ("InputForm")
the Click procedure of the OK-button has to call Me.Visible = False to just hide the dialog.
the Click procedure of the Cancel-button has to call DoCmd.Close acForm, Me.Name to close the dialog.

Msgbox is not getting displayed in outlookApp_Send event

I am sending mail using outlook, on send event I am trying to display message, but msgbox is not getting displayed till I clicked on screen( anywhere).
(Once user click on Send in Outlook, I am changing the Outlook windowState to minimize.)
Public Sub oApp_ItemSend(ByVal Item As Object, Cancel As Boolean)
If(StrComp(clickedEvent, "ClickMe", vbTextCompare) = 0) Then
MsgBox "HI"
end if
End sub
Thanks
This is a known issue with several MS Office applications. Try adding this code and see if it helps:
AppActivate Application.Caption
or
AppActivate "Microsoft Excel" ' name of application that needs to be in focus
From the documentation:
The AppActivate statement changes the focus to the named
application or window but does not affect whether it is maximized or
minimized.

Display Access form on open & update textbox *while running other code*

Sometimes trying to accomplish the most minor turns into a major headache... I somehow corrupted a database while experimenting to solve this issue. Now I'm really determined to figure this out. (I'll worry about the corrupt DB later!)
End Goal:
When opening .accdb file, automatically:
open form unbound)
run code to:
get Last Update Date from table
display Last Update Date in textbox on form (must be visible to user)
if table data is outdated, run code to:
update data in table
display Update Progress Message(s) in textbox on form (must be visible to user)
when finished:
display Last Update Date in textbox on form (must be visible to user)
The problem is the goal of (must be visible to user).
I can't get the form to display anything until all the code's finished running, regardless of how I run the code (including: set the form to auto-open with File → Options → Display Form; add an AutoExec macro to call code to open the form and run the update; or, run it manually.)
I've been experimenting with the form's opening events to see if I missed something.
According to the Order of Events documentation:
Working with data on a form
Form and control events occur as you move between records in the form and change data. For example, when you first open a form, the following sequence of events occurs:
Open (form) → Load (form) → Resize (form) → Activate (form) → Current (form) → Enter (control) → GotFocus (control)
At no point does the form visibly update. I've tried adding Me.Repaint and even tried Echo=True and DoEvents but obviously they didn't change anything.
My code for testing:
Private Sub Form_Open(Cancel As Integer)
txtTest = "Form_Open()": MsgBox "1. Form_Open"
End Sub
Private Sub Form_Load()
txtTest = "Form_Load()": MsgBox "2. Form_Load"
End Sub
Private Sub Form_Current()
txtTest = "Form_Current()": MsgBox "3. Form_Current"
End Sub
Private Sub txtTest_Enter()
txtTest = "txtTest_Enter()": MsgBox "4. txtTest_Enter"
End Sub
Private Sub txtTest_GotFocus()
Echo True : Repaint : DoEvents
txtTest = "txtTest_GotFocus()": MsgBox "5. txtTest_GotFocus"
End Sub
Private Sub txtTest_Change() : MsgBox "This Does't Run!" : End Sub
Private Sub Form_AfterUpdate() : MsgBox "This Does't Run!" : End Sub
Private Sub txtTest_Change() : MsgBox "This Does't Run!" : End Sub
Private Sub txtTest_BeforeUpdate(Cancel As Integer) : MsgBox "This Does't Run!" : End Sub
Private Sub txtTest_AfterUpdate() : MsgBox "This Does't Run!" : End Sub
The result of opening the form:
(Note that the form's Activate and AfterUpdate events and the control's Change, BeforeEvent and AfterEvent events do not fire at all.)
Theoretically, the form should be visible behind the MsgBox, but neither running the actual update code, nor running a "pause" (loop with DoEvents) doesn't display the form either.
I could swear I've done this in the past without issue but no matter what I do try, the form only displays once all code is finished running.
You can use the Form_Timer event to delay execution until the form fully finishes loading.
Private Sub Form_Load()
Me.TimerInterval = 1 'Trigger 1 millisecond, but asynchronously from other tasks
'Triggers after AutoExec macro has finished
End Sub
Private Sub Form_Timer()
Me.TimerInterval = 0 'Trigger only once
'Your other tasks here
End Sub

Why does Showing a UserForm as Modal Stop Code Execution?

The following VBA code stops at Me.Show. From my tests, it seems that Me.Show stops all code execution, even if the code is inside the UserForm.
This part is outside the UserForm:
Public Sub TestProgress()
Dim objProgress As New UserForm1
objProgress.ShowProgress
Unload objProgress
End Sub
This part is inside the UserForm:
Private Sub ShowProgress()
Me.Show vbModal
Dim intSecond As Integer
For intSecond = 1 To 5
Application.Wait Now + TimeValue("0:00:01")
Me.ProgressBar1.Value = intSecond / 5 * 100
Next intSecond
Me.Hide
End Sub
The code stops at Me.Show, after the UserForm is displayed. There is no error, it just discontinues executing code. It seems that the only way to execute code inside a modal UserForm in VBA is to include it in the UserForm_Activate procedure like this:
This part is outside the UserForm:
Public Sub TestProgress()
Dim objProgress As New UserForm1
Load objProgress
Unload objProgress
End Sub
This part is inside the UserForm:
Private Sub UserForm_Initialize()
Me.Show vbModal
End Sub
Private Sub UserForm_Activate()
Dim intSecond As Integer
For intSecond = 1 To 5
Application.Wait Now + TimeValue("0:00:01")
Me.ProgressBar1.Value = intSecond / 5 * 100
Next intSecond
Me.Hide
End Sub
Of course, I can't put Me.Show inside UserForm_Activate because that procedure only fires after the UserForm Show event.
The documentation for UserForm.ShowModal says "When a UserForm is modal, the user must supply information or close the UserForm before using any other part of the application. No subsequent code is executed until the UserForm is hidden or unloaded."
I am trying to use a modal UseForm as a progress bar to prevent the user from interacting with the application while a process runs. But this will be difficult to accomplish if all my code has to be within the UserForm_Activate procedure.
Am I missing something here? Why does all code execution stop at Me.Show?
When the form is displayed with vbModal, the code will suspend execution and wait for user interaction with the form. For example clicking a button or using a dropdown.
If you update the form property
ShowModal = False
and remove vbModal from your code. This will allow code execution to continue when the form is displayed.
I was searching for an answer to why I was getting the following error:
Run time error '5': Invalid procedure call or argument
when running this line of code:
UserForm1.Show True
even though this line works:
UserForm1.Show False
Of course. True is not the same as vbModal! So the simple answer is to use the correct enumerations:
UserForm1.Show vbModal
UserForm1.Show vbModeless
I think I figured this out.
After Me.Show the UserForm_Activate event fires. If there is no code in the UserForm_Activate procedure nothing will happen because VBA is waiting for Me.Hide.
So the order of events is: Me.Show > UserForm_Activate > Me.Hide
Any code that I want to run must be in the UserForm_Activate procedure and must be before Me.Hide.
The structure is very strict, but I may be able to use that structure to my advantage.