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

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

Related

In LibreOffice Calc, how do I change through LibreOffice Basic the value of a cell with an event listener set to it without crashing the program?

I am trying to create two tables which mirror changes made to any of them to one another automatically.
To that end, I added event listeners which are triggered when the cells of these tables are edited by the user.
Unfortunately, editing one of the tables causes LibreOffice to crash, even though the changes are indeed reflected correctly, as seen upon reopening the file.
I thought the crash might be due to a never-ending circular reference, but it still crashes after it has been made non-circular (by commenting out the relevant parts of the code so that changes are reflected only one way rather than both ways).
I noticed the code worked fine when writing to a cell that didn't have an event listener set to it.
How can I write to one of the cells with event listeners set to them without causing LibreOffice to crash?
You may want to download the following file. Please run Main and then try editing the cell C3 of the Planning sheet. The arbitrary string "C" should be written in the cell C4 of the Services sheet.
Here is a simplified version of the code :
REM ***** BASIC *****
const SERVICESSHEET_NUMBER = 2
const SERVICESSHEET_SERVICES_COLUMN = 2
Type cellStruct
columnNumber As Integer
rowNumber As Integer
End Type
Sub UpdateServicesSheet(editedCell As cellStruct, newValue As String)
Dim oSheets
Dim servicesSheet
oSheets = ThisComponent.getSheets()
servicesSheet = oSheets.getByIndex(SERVICESSHEET_NUMBER)
servicesSheet.getCellByPosition(SERVICESSHEET_SERVICES_COLUMN, 3).setString(newValue)
End Sub
Private oListener, cellRange as Object
Sub AddListener
Dim sheet, cell as Object
sheet = ThisComponent.Sheets.getByIndex(0) 'get leftmost sheet
servicesSheet = ThisComponent.Sheets.getByIndex(2)
cellRange = sheet.getCellrangeByName("C3")
oListener = createUnoListener("Modify_","com.sun.star.util.XModifyListener") 'create a listener
cellRange.addModifyListener(oListener) 'register the listener
cellRange = servicesSheet.getCellrangeByName("C4")
oListener = createUnoListener("Modify_","com.sun.star.util.XModifyListener") 'create a listener
cellRange.addModifyListener(oListener) 'register the listener
End Sub
global CircularReferenceAllowed As boolean
Sub Modify_modified(oEv)
Dim editedCell As cellStruct
Dim newValue As String
editedCell.columnNumber = 2
editedCell.rowNumber = 2
If CircularReferenceAllowed Then
CircularReferenceAllowed = false
UpdateServicesSheet(editedCell, "C")
End If
End Sub
Sub Modify_disposing(oEv)
End Sub
Sub RmvListener
cellRange.removeModifyListener(oListener)
End Sub
Sub Main
CircularReferenceAllowed = true
AddListener
End Sub
Crossposted to :
OpenOffice forums
LibreOffice discourse platform
It seems like the event trigger is within another event's function is causing the crash. In any case, the solution is to remove the listener, then add it back after modifying the other cell.
You do need to global the Listener and the Cell objects to make this work.
This code is simplified to work on C3 and C15 on the first sheet. It would also output some information on C14, which isn't really necessary for your purpose, but I use it to see what's happening. You need to adopt the according to what you need.
global goListener as Object
global goListener2 as Object
global goCellR as Object
global goCellR2 as Object
global goSheet as Object
global giRun as integer
global giUpd as Integer
Sub Modify_modified(oEv)
Dim sCurStr$
Dim sNewStr As String
'xRay oEv
giRun = giRun + 1
sCurStr = oEv.source.string
oCell = goSheet.getCellByPosition(2, 14)
If (oCell.getString() <> sCurStr) Then
' only update if it's different.
giUpd = giUpd + 1
goCellR2.removeModifyListener(goListener2)
oCell.setString(sCurStr)
goCellR2.addModifyListener(goListener2)
End If
sNewStr =sCurStr & " M1 Run=" & giRun & " Upd=" & giUpd
goSheet.getCellByPosition(2, 13).setString(sNewStr)
End Sub
Sub Modify2_modified(oEv)
Dim sCurStr$
Dim sNewStr As String
Dim oCell as Object
'xRay oEv
giRun = giRun + 1
sCurStr = oEv.source.string
oCell = goSheet.getCellByPosition(2, 2)
If (oCell.getString() <> sCurStr) Then
' only update if it's different.
giUpd = giUpd + 1
goCellR.removeModifyListener(goListener)
oCell.setString(sCurStr)
goCellR.addModifyListener(goListener)
End If
sNewStr =sCurStr & " M2 Run=" & giRun & " Upd=" & giUpd
goSheet.getCellByPosition(2, 13).setString(sNewStr)
End Sub
Sub Modify_disposing(oEv)
MsgBox "In Modify_disposing"
End Sub
Sub Modify2_disposing(oEv)
MsgBox "In Modify2_disposing"
End Sub
Sub RmvListener
MsgBox "In RmvListener"
goCellR.removeModifyListener(goListener)
goCellR2.removeModifyListener(goListener2)
End Sub
Sub AddListener
goSheet = ThisComponent.Sheets.getByIndex(0) 'get leftmost goSheet
'servicesSheet = ThisComponent.Sheets.getByIndex(2)
goCellR = goSheet.getCellrangeByName("C3")
goListener = createUnoListener("Modify_","com.sun.star.util.XModifyListener") 'create a listener
goCellR.addModifyListener(goListener) 'register the listener
goCellR2 = goSheet.getCellrangeByName("C15")
goListener2 = createUnoListener("Modify2_","com.sun.star.util.XModifyListener") 'create a listener
goCellR2.addModifyListener(goListener2) 'register the listener
End Sub
Sub Main
giRun = 0
giUpd = 0
AddListener
End Sub

Recordset Addnew modifies first record in a table

I'm currently having problems with adding a new record in a table through VBA in Access. The VBA is used through a Button in a Form which has 2 Combination fields and 2 date fields.
Private Sub Schlussel_hinzufügen_Click()
On Error GoTo ErrHandler
Dim R As Recordset
Set R = CurrentDb.OpenRecordset("Schluesselhistorie")
R.AddNew
' Normally data is added to the record between these two
R.Close
Me.Requery
DoCmd.Close
Exit Sub
ErrHandler:
MsgBox "Couldn't save record!", vbCritical
End Sub
As soon as the R.AddNew is called the first record of the table is modified with the Data from the combination and date fields. A completely new record at the end of the table is created as well when
R![SLH_Schluessel_ID] = Me.Kombinationsfeld13.Value
R![SLH_Kontakt_ID] = Me.Kombinationsfeld15.Value
R![SLH_Datum_Ausgabe] = Me.SLH_Datum_Ausgabe.Value
R![SLH_Datum_Rueckgabe_Soll] = Me.SLH_Datum_Rueckgabe_Soll.Value
R.Update
is called though. I am kinda irritated as the former (first row event) shouldn't happen as I know and when code is added above both the first row is modified and a new record with these values is added.
The table is externally linked and the field names are in German.
Are there restrictions to the DAO where the Recordset used can't specify which line the Addnew should use. Or does the Addnew take the values of the Form to automatically add the Values to the table?
You should use the recordsetclone of the form:
Private Sub Schlussel_hinzufügen_Click()
On Error GoTo ErrHandler
Dim R As DAO.Recordset
Set R = Me.RecordsetClone
R.AddNew
' Normally data is added to the record between these two
R.UpDate
R.Close
Exit Sub
ErrHandler:
MsgBox "Couldn't save record!", vbCritical
End Sub

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

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

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

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