Excel VBA Form is not refreshing properly when a subform is closed - forms

Good Afternoon. I've running into a situation I hope someone here can help with. I'm running Office 2010 on Windows XP and have an Excel worksheet that contains a button to show a modal form (Form01). Form01 contains 3 Listboxes of data. Double-Clicking a item in listbox2 will open another modal form (Form02) so the item can be modified. Unloading Form02 will save the data and call a couple of macros to adjust a named range on the host worksheet. This code is stored in a Module, not on the form.
This is where the problem occurs. When Form02 is unloaded and Form01 is accessable, I cannot select anything in listbox1 or listbox3 or buttons on Form01. I have to first select something in listbox2, then I have access to the other controls on the form. I've tried to .SetFocus on the other controls and add DoEvents after the Form02.Show 1 statement with no luck. The only workaround I've found is to hide and reshow Form01 which causes the screen to flicker. Application.ScreenUpdating = False and back to True doesn't seem to help either. I really need to figure out what the other controls are not accessible when Form02 is shown and then closed. Has anyone else experienced this behaviour or might have a suggestion?

Change this
Private Sub lstSiteMaster_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
If lstSiteMaster.ListCount = 0 Then Exit Sub
LoadFrmEditDataset Me.Controls("lstSiteMaster"), "SITE MASTER"
frmEditDataset.Show 1
Unload frmEditDataset
DoEvents
Me.lstSiteMaster.ListIndex = -1
Me.lstSiteList.ListIndex = -1
Me.lstMiniPOR.ListIndex = -1
'''BounceTheForm
End Sub
to
Private Sub lstSiteMaster_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
If lstSiteMaster.ListCount = 0 Then Exit Sub
LoadFrmEditDataset Me.Controls("lstSiteMaster"), "SITE MASTER"
frmEditDataset.Show 1
DoEvents
Me.lstSiteMaster.ListIndex = -1
Me.lstSiteList.ListIndex = -1
Me.lstMiniPOR.ListIndex = -1
'''BounceTheForm
End Sub
AND
this
Private Sub cmdApply_Click()
With Me
UpdateDataset .lblDataset, .lblECR, .lblFA, .lblFieldId, .txtFieldValue, .cboAction, .lblIndex + 2
End With
Me.Hide
End Sub
to
Private Sub cmdApply_Click()
With Me
UpdateDataset .lblDataset, .lblECR, .lblFA, .lblFieldId, .txtFieldValue, .cboAction, .lblIndex + 2
End With
Unload Me
End Sub

Related

Keeping information on Form B from Form A when Form A is closed

I was wondering if there is any code that lets some information you enter on Form A remain in the textboxes in Form B when Form A is closed. Im hoping to keep some user information entered into the first form on the second form and still have the first form close.
Thank you!
You can't maintain value from field when you close a form. So you can create some Global vars to copy his value and maintain on memory.
Sometimes i use another option: hide the form A meanwhile you use form B, then when you need to close Form B, simply check if A is open and then close it.
Function FIsLoaded(stFrmName$) As Integer
Dim I As Integer
For I% = 0 To Forms.Count - 1
If (Forms(I%).FormName = stFrmName$) Then
FIsLoaded = True
Exit Function
End If
Next I%
For I% = 0 To Reports.Count - 1
If (Reports(I%).FormName = stFrmName$) Then
FIsLoaded = True
Exit Function
End If
Next I%
FIsLoaded = False
End Function
With this function you can do before close form:
if fisloaded("formA") then
DoCmd.Close acForm ,"formA"
end if

Wanting to allow only 2 of the same form to be opened VB6

So far i have some code that allows a user to Hit F1 which loads a new form of the same properties and then hides the one the first one they had up, Hitting F2, allows the user to close the newly opened form and show the one they opened first. I would like a restriction that allows the user to open only 1 extra form if they hit F1 with 2 of the same forms open then a messagebox appears telling them to close the second form first otherwise allow it to be opened.
Here is what i have so far.
Private Sub Form_Load()
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyF1
'hides the current form
Me.Hide
'loads a new form with the same properties
Dim f As New Form1
Load f
'shows this new form
f.Show
'says that the second form is open
fOpen = True
Case vbKeyF2
'closes the second form
Unload Me
'says that the second form is closed
fOpen = False
'shows the first form you were on
Form1.Show
End Select
End Sub
Private Sub Form_QueryUnload(cancel As Integer, unloadmode As Integer)
'if your hitting "X" on second form then just close form2
If fOpen = False Then
Form1.Show
Else
'if your hitting "X" on main form close everything
Unload Me
End If
End Sub
Maybe something like if fOpen = true then disallow the user to hit F1? Not quite sure, but im close.
Forgive me if my VB6 is a little off, but you need to enumerate though the Forms collection to check to see if your form is already open...
Dim frm As Form
For Each frm In Forms
If frm.Name = "myForm" Then frm.Show()
Next frm
See this.
-- EDIT --
Just while I think on, to tune your code you could use a numeric iteration...
Dim f As Integer
Dim t As Integer
t = Forms.Count - 1
For f = 0 To t
If Forms(f).Name = "myForm" Then Forms(f).Show()
Next frm
-- EDIT 2 --
Just a further note on this. You may also want to introduce a counter so that you can check to see if there are two fields as in your original post...
Dim frm As Form
Dim c As Integer
For Each frm In Forms
If frm.Name = "myForm" Then
c = c + 1
If c = 2 Then
frm.Show()
Exit For 'Speed up the search if there are lots of forms
End If
End if
Next frm

How to show form at start of function which closes at end of function?

I have a function which may take some time to execute.
How can I have a small modal form to show at the start of the function which closes when the function finishes?
Say frmModal is the form you wish to show. At the start of your function put in
frmModal.Show
frmModal.refresh
At the end of your function put in
Unload frmModal
My favorite trick for this is to put the code that is run into the form that is displayed while it is running. Then when it is done call Unload Me
'Code in Form1
Call frmWait.Show(vbModal, Me)
'Code in frmWait
Private Sub Form_Activate()
'Do some work ...
Unload Me
End Sub
when you load the form modal (form1.show vbmodal) then subsequent code is not executed until the model form is closed
a simple way (without api) to simulate what you want is to show the form modeless, and temporary disable the other form
have a look at the differences between command1 and command2 in the following test project :
'3 forms :
' Form1 : name=Form1
' contains 2 command buttons with the name Command1 and Command2
' Form2 and Form3 contain nothing special
Option Explicit
Private Sub Command1_Click()
Dim lngEnd As Long
Form3.Show vbModal
lngEnd = Timer + 5
Do While Timer < lngEnd
Caption = CStr(Timer)
DoEvents
Loop
Unload Form3
End Sub
Private Sub Command2_Click()
Dim lngEnd As Long
Enabled = False
Form2.Show vbModeless, Me
lngEnd = Timer + 5
Do While Timer < lngEnd
Caption = CStr(Timer)
DoEvents
Loop
Enabled = True
Unload Form2
End Sub

How do I cycle to the previous control in VB6?

So I have a bunch of listboxes on my VB form. I created a command button too.
I want to make it so that if I press the command button it cycles from the current listbox, which has focus, to the previous listbox. It's the equivalent of using TAB and SHIFT TAB to cycle amongst the control. SHIFT TAB goes backwards in the cycle, TAB goes forward in the cycle. I vaguely remember there was a way to go back & forth in the "tab cycle".
How do I do this?
You can;
Private mCurrentListboxTabIndex As Integer
'wire up, simpler if you use a control array
Private Sub List1_GotFocus()
mCurrentListboxTabIndex = List1.TabIndex
End Sub
Private Sub List2_GotFocus()
mCurrentListboxTabIndex = List2.TabIndex
End Sub
Private Sub List3_GotFocus()
mCurrentListboxTabIndex = List3.TabIndex
End Sub
Private Sub btnPrev_Click()
FocusListBoxByTabIndex -1
End Sub
Private Sub btnNext_Click()
FocusListBoxByTabIndex 1
End Sub
Private Sub FocusListBoxByTabIndex(offset As Long)
Dim ctrl As VB.Control
For Each ctrl In Me
If TypeOf ctrl Is ListBox Then
If ctrl.TabIndex = mCurrentListboxTabIndex + offset Then
ctrl.SetFocus
Exit Sub
End If
End If
Next
End Sub

Execute code when form is closed in VBA (Excel 2007)

I would like to execute some code when a user closes a form using the x button in the top right corner of the window (I have the form load when the excel spreadsheet is opened, and it hides Excel. I want to exit excel once the form is closed, or at least show excel again so the user may exit it manually)
Looking at the form properties, the Unload property is not present, nor am I able to figure out how to make a function which executes when the form is closed.
Unfortunately, coding this in VB is not an option, it must be VBA.
I'm aware of the code needed to unhide Excel or quit it outright, just not how to tie it to the unload event.
You can use QueryClose event of the UserForm as follows:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = 0 Then
' Your codes
' Tip: If you want to prevent closing UserForm by Close (×) button in the right-top corner of the UserForm, just uncomment the following line:
' Cancel = True
End If
End Sub
You can also use vbFormControlMenu like this:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
'Your code goes here
End If
End Sub
A colleague was able to provide the answer, including example here for everybody else
Private Sub userform_terminate()
'Code goes here
End Sub
You can use Unload Me in VBA to close a form. Just put the code to close Excel immediately following that.
Private Sub Form_Unload(Cancel As Integer)
Dim msgRes As VbMsgBoxResult
msgRes = MsgBox("Exit form ?", vbYesNo)
If msgRes = vbYes Then
'optional code
ElseIf msgRes = vbNo Then
Cancel = True
End If
End Sub
I was able to prevent the form from closing when the X button was click using the following:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Cancel = MsgBox("Please confirm cancellation", vbOKCancel + vbQuestion) = vbCancel
End Sub
try something like this:-
Private Sub Form1_FormClosing(sender as Object, e as FormClosingEventArgs) _
Handles Form1.FormClosing
//Code you want to execute
End Sub