MS Access Time Delay for Form VBA - database

I have created a message box that is a like a reminder and pops up when my switchboard opens on my database.
the message box tells me how many customers havent paid for their order in 25 days. I have put the code into the On Load Section of the switchboard.
However, when i click into my switchboard the code runs instantly which is expected and the message box appears saying "There are x uncompleted payments that have not been paid in 25 days, would you like to see these now" and there is a yes or no button.
But when this box loads and i click yes my switchboard and query opens at the same time and it automatically directs me to the switchboard and not the query that i want to see on my screen. I am aiming to have the main menu open, delay the code for a certain amount of seconds then opens the query if i select yes.
This is the code i have produced.
Public Sub Form_Load()
Dim time1, time2
time1 = Now
time2 = Now + TimeValue("0:00:05")
Do Until time1 >= time2
time1 = Now()
Loop
Dim OS As Integer
OS = DCount("[Paid]", "[OutstandingPayments]", "DateDiff('d', DateOrder,
Now()) > 25")
If OS = 0 Then
Exit Sub
Else
If MsgBox("There are " & OS & " uncompleted Payments that have not been paid in 25 days" & _
vbCrLf & vbCrLf & "Would you like to see these now?", _
vbYesNo, "You Have Uncomplete Payments...") = vbYes Then
DoCmd.Minimize
DoCmd.OpenQuery "OutstandingPayments", acNormal
Else
Exit Sub
End If
End If
End Sub
Basically what this code does is just delay the code being produced but i thought the switchboard could open showing the screen then the code would run and then display my message box.
is there a way i can be able to open switchboard on load, have it loaded then for my code to run to display my message box.
Gif attached to illustrate problem
https://gyazo.com/94f7e56cbe9220673d10a810e5282dda
Thank you.

You can use the Form.Timer property to set a timer, and execute the code after that timer.
Private Sub Form_Load()
Me.TimerInterval = 5000 '5000 milliseconds
'Do other stuff
End Sub
Private Sub Form_Timer()
Me.TimerInterval = 0 'Disable timer from running again
'Do stuff
End Sub
Also, if you're going to delay using a while loop, fill it with DoEvents + a sleep function (like this one) to prevent it from locking down your program.

Related

Get Msgbox 3mins later after clicking a commandbutton1 vba

I am really stuck with one of my project. I need to get msgbox appearing 3mins later once clicked on commandbutton1.
I have a button which copies the text "I will be back in 3minutes" now I need to have a pop msg after 3minutes reminding user with popmsg "your time is up"
I tested 10 seconds interval, and it worked.
Private Sub CommandButton1_Click()
'any previous stuff
Application.OnTime Now + TimeValue("00:00:10"), "RemindMe"
End Sub
The following subroutine is in a module.
Sub RemindMe()
MsgBox "your time is up!"
End Sub
Meanwhile, the Excel window doesn't get frozen, you can still operate on the worksheet.

VBA Excel 2013: Assign Array Values from Another UserForm

I'm relatively new to VBA. I have a program I'm writing where the user is given the option to change their input from a 2 dimensional array in another user form.
The first user form, UserForm1, allows the user to input the information from text fields and saves it to the respective array row, i, when pressing a Save command button.
When the user presses an OK command button, the user is asked if they want to add another set of data. If they say no, they are asked if they want to change data. If they say yes, then another user form, UserForm2, is opened.
The code for UserForm1 is similar to the code below:
Public MyArray as Variant, i as Integer
Sub Userform_Initialize()
ReDim MyArray(100,4)
End Sub
Sub SaveButton_click()
MyArray(i, 1) = TextField1.Value
MyArray(i, 2) = TextField2.Value
MyArray(i, 3) = TextField3.Value
MyArray(i, 4) = TextField4.Value
End Sub
Sub OKButton_click()
If msgbox("Do you want to add more data?", vbYesNo) = vbNo Then
If msgbox("Do you have corrections to be made?",vbYesNo) = vbYes Then
Load UserForm2
UserForm2.Show
Else: Exit Sub
End If
Else: i = i + 1
Exit Sub
End If
End Sub
In UserForm2, the user chooses the row number, i, from a combo box. When the row number is selected, the array information is automatically populated in text fields from UserForm1.
When the user presses the Save command button, it should pass the information from the text fields and write it to the respective row.
The code for UserForm2 is similar to the code below:
Public j as integer
Sub Userform_Initialize()
For j = 1 to UserForm1.i
ComboBox1.AddItem (j)
Next
End Sub
Sub SaveButton_click()
UserForm1.MyArray(ComboBox1.Value, 1) = TextField1.Value
UserForm1.MyArray(ComboBox1.Value, 2) = TextField2.Value
UserForm1.MyArray(ComboBox1.Value, 3) = TextField3.Value
UserForm1.MyArray(ComboBox1.Value, 4) = TextField4.Value
End Sub
Stepping through the code, the values from MyArray should be properly referenced, and I can see the values initially saved from UserForm1. However, the values are not changing as I step to the next line.
Does anyone have a solution for my problem? Thank you in advance for your help!
I believe I found my solution. I had to declare the array in the module containing the code to start the program as a public variable. After I did that and modified the code, the values were written properly to the array.
If anyone has other solutions, though, I would like to know. I'm not that experienced with VBA, so I want to hear other solutions.

Conditional Formatting VBA

I am building a form to enter account information and order status. Each row needs to change based on the value of one of the cells on the same row, on this case cell "H". I can easily achieve this with conditional formatting but I think this makes the file bigger than programming code. I have tried some options but I can tell at this moment I'm way lost. I am attaching an example of what I want to accomplish. I don't know what to do at this point so if someone can help me I would really appreciate it.
A Worksheet_Change event macro¹ deals with, well, changes on the worksheet.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Columns("H")) Is Nothing Then
On Error GoTo bm_Safe_Exit
Application.EnableEvents = False
Dim trgt As Range
For Each trgt In Intersect(Target, Columns("H"))
Select Case LCase(trgt.Value2)
Case "credit"
Cells(trgt.Row, "A").Resize(1, 12).Interior.ColorIndex = 45
Case "completed"
Cells(trgt.Row, "A").Resize(1, 12).Interior.ColorIndex = 10
Case Else
Cells(trgt.Row, "A").Resize(1, 12).Interior.Pattern = xlNone
End Select
Next trgt
End If
bm_Safe_Exit:
Application.EnableEvents = True
End Sub
¹ An event macro belongs on a Worksheet or Workbook code sheet; not a Module code sheet. For a worksheet code sheet, right click the worksheet's name tab and choose View Code. When the VBE opens, it will have the worksheet code sheet (typically titled something like Book1 - Sheet1 (Code)) in the foreground. Paste the code in and make any personalizing adjustments then tap Alt+Q to return to the worksheet.

MS Access OpenForm was cancelled - error 2501

I've been asked to make some modification to Forms in an Access database used by a group I work with. I had to add a data entry form similar to one that exists so I copied one of the existing forms and made some modifications. When I click the button to open the form to test it I get this error:
Run-time error '2501'
The OpenForm action was cancelled.
Here is the code that is called to cause this error:
Private Sub cmdCPE_EntryForm_Click()
On Error GoTo Err_cmdCPE_EntryForm_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "CPE_EntryForm"
DoCmd.OpenForm stDocName ', , , stLinkCriteria
Exit_cmdCPE_EntryForm_Click:
Exit Sub
Err_cmdCPE_EntryForm_Click:
MsgBox Err.Description
Resume Exit_cmdCPE_EntryForm_Click
End Sub
Thx in advance for any assitance.
the form you're trying to open is in design view. Close design view
I got this error when my form was referring to a query that no longer existed.
Once I updated the form to point to an existing query, my code worked fine.
My form open error 2501 "Form Open Cancelled" error in MSAccess was due to the form's data source table not existing.
Runtime Error 2501 can occur on calling the OpenForm method if there is a problem with the database.
Try following the steps in this link under the headings "Recovering from corruption" and "Symptom: Cannot open a form or report"?
http://allenbrowne.com/ser-47.html
Could also be a problem with the new form's Open or Load events, might be worth posting that code as well for review.
I had exactly the same error when I wanted to open a form containing a sub form whose data was located in another folder. By decreasing the path of the dorsal base this has repaired the error. For example, the first path was C:\Dev\Application\Vers1\Prog\Debug\myAppDB.accdb, so I decreased the path's length in : C:\Dev\Debug\myAppDB.accdb and this solved the problem. The same error occurred with a network path, and I fixed it with the same method.
By assuming that you have :
a command button named "cmdOpenForm" and
a form called: "myForm"
I have also add this code on the On Click Event of the command button :
Private Sub cmdOpenForm_Click()
On Error GoTo Err_cmdOpenForm_Click
If CurrentProject.AllForms("myForm").IsLoaded = True Then
DoCmd.Close acForm, "myForm"
Else
DoCmd.OpenForm "myForm", acNormal, , , , acWindowNormal, Me.Form.Name
End If
Exit_cmdOpenForm_Click:
Exit Sub
Err_cmdOpenForm_Click:
MsgBox "Error number : " & Err.Number & vbCrLf & "Error: " & Err.Description, vbCritical
Resume Exit_cmdOpenForm_Click
End Sub
Your RecordScorce is not recognized. Type your recordScource In Code, then in Menu Tool\Reference Change place of your references.Then Debug your project And then save it.
Try closing the forms that you want to open then rerun the code. Worked for me.

Visual basic 6 issue with combobox

I have created a forum in vb6 and made connection with a database in access,everything went perfect.
my problem is in my form there is 2 combobox one to select Number and other to get me other numbers (watch the video to understand )
anyway the first combo is working and the second combo is working too but after selecting different number from the first combo i don't get anything in the second combo.anyway i know i just miss something in the code something very stupid
i have uploaded a video so you can see my problem, thanks in advance.
Private Sub Form_Load()
liaison
Do Until rspatient.EOF
Me.npa.AddItem rspatient.Fields(0)
rspatient.MoveNext
Loop
End Sub
Private Sub npa_Click()
rspatient.MoveFirst
Dim cr As String
cr = "npation ='" & npa & "'"
rspatient.Find cr
nom = rspatient.Fields(1)
prenom = rspatient.Fields(3)
rshospita.MoveFirst
nh.Clear
While rshospita.EOF = False
If UCase(rshospita.Fields(14)) = UCase(npa) Then
nh.AddItem rshospita.Fields(0)
End If
rshospita.MoveNext
Wend
End Sub
video for more detail :
https://www.youtube.com/watch?v=Tidm18_tvp0
The simplest possible reason for your problem is that you don't have any hospital records associated with your second patient. Check that first.
However, your code is also a bit convoluted. A simpler way to do what you want is to use Filter instead of Find. Filter restricts your recordset to only those records which match the filter, so you can simply iterate the filtered recordset the same way you do the whole one (rspatient). Something like this:
Private Sub npa_Click()
With rshospita
.Filter = ".Fields(14) = '" & npa & "'"
.MoveFirst
nh.Clear
Do Unitl .EOF
nh.AddItem .Fields(0)
.MoveNext
End With
End Sub

Resources