In our Visual Basic 6 application, one of the forms has a picture box that displays a photograph (stored in the database in bitmap format). During the form load process, the picture box change event fires, as expected, at the point when the picture is loaded. However immediately after the form load process completes, the picture box change event fires again. There is code in that change event that we DO NOT want to execute at that point in the program. So the question is, what is causing the change event to execute at that point in the code and how can we get control of it?
Here is the code (the form_load subroutine calls SetMainMenuComboBoxSelecti
on (with source=TOOLBAR) , which calls ChildCbox_click, which calls LoadPhoto) The picture1_change event fires at "Set DATA2.Recordset = photoRS" in the LoadPhoto subroutine, then it fires again immediately after control has returned to form_load and LOAD_EXIT: Exit Sub has executed. (This code was working correctly in the past. In our latest program version we upgraded to DAO 3.6, and we had to add a recordset to get our data control to work. We can't think of any reason that would cause this problem.)
Private Sub form_Load()
Dim Action As Integer
On Error GoTo LOAD_ERROR
Screen.MousePointer = 11
' no tabstops
' set common controls: first control on form to set focus
' combobox on main menu
Set mycbox = frmMainMenu.cboxProfile
SSTab1.Tab = 0
SSTab1.TabVisible(4) = False
Set myfirstfocus = picTfirst_name
'Load form procedure (formname, C=Child D = Dialog, Width, Height)
Call LoadFormProc(Me, "C", 9525, 5195) 'H changed from 5100, W changed from 9480 9/15/04 Lou
'mainmenu combo box
Call LoadComboProc("consumers",
mycbox)
LoadSmartCombos
'Load Dumb Combos
Call LoadDumbCombos
'Form SQL--this is the SQL used throughout the form
Formsql$ = "select * from [BACKGROUND (CONSUMER)] "
wclause$ = " WHERE [SOCIAL SECURITY NUMBER] = '" & ssn(str$(mycbox.ItemData(m
ycbox.List
Index))) & "';"
'Prepare International
SetMyWording
countForm = countForm + 1
Call SetMainMenuComboBoxSelecti
on(mycbox,
Me)
LOAD_EXIT:
Screen.MousePointer = 0
Exit Sub
LOAD_ERROR:
Action = errors(Err)
Select Case Action
Case 0
Resume
Case 1
Resume Next
Case 2, 3
GoTo LOAD_EXIT
End Select
End Sub
Public Sub SetMainMenuComboBoxSelecti
on(c As Control, f As Form)
'This routine will fill a combo box (consumer name) on the
'main menu with the same consumer on the previous form
'1 a toolbar button for consumer information
' was pressed and
'2 there is a name selected in cbox_xxxxx
Dim matchSSN As Long, i%
If source = "TOOLBAR" And countForm > 1 Then 'if user click on the shortcut
'THERE IS A SELECTION IN frmMainMenu.cboxProfile
Select Case frmMainMenu.ActiveForm.Nam
e
Case "frmProfile"
matchSSN = frmMainMenu.cboxProfile.It
emData(frm
MainMenu.c
boxProfile
.ListIndex
)
Case "frmBudget"
matchSSN = frmMainMenu.cboxBudget.Ite
mData(frmM
ainMenu.cb
oxBudget.L
istIndex)
Case "frmEducation"
matchSSN = frmMainMenu.cboxEducation.
ItemData(f
rmMainMenu
.cboxEduca
tion.ListI
ndex)
Case "frmEmployment"
matchSSN = frmMainMenu.cboxEmployment
.ItemData(
frmMainMen
u.cboxEmpl
oyment.Lis
tIndex)
Case "frmIncident"
matchSSN = frmMainMenu.cboxIncident.I
temData(fr
mMainMenu.
cboxIncide
nt.ListInd
ex)
Case "frmMedications"
matchSSN = frmMainMenu.cboxMedication
s.ItemData
(frmMainMe
nu.cboxMed
ications.L
istIndex)
Case "frmNotes"
matchSSN = frmMainMenu.cboxNotes.Item
Data(frmMa
inMenu.cbo
xNotes.Lis
tIndex)
Case "frmRelationships"
matchSSN = frmMainMenu.cboxRelationsh
ips.ItemDa
ta(frmMain
Menu.cboxR
elationshi
ps.ListInd
ex)
Case "frmRoutines"
matchSSN = frmMainMenu.cboxRoutines.I
temData(fr
mMainMenu.
cboxRoutin
es.ListInd
ex)
Case "frmServiceParticipation"
matchSSN = frmMainMenu.cboxProgramPar
ticipation
.ItemData(
frmMainMen
u.cboxProg
ramPartici
pation.Lis
tIndex)
End Select
For i = 0 To c.ListCount - 1
If c.ItemData(i) = matchSSN Then
c.ListIndex = i
Call f.ChildCboxClick
Exit For
End If
Next
source = ""
End If
End Sub
Public Sub ChildCboxClick()
Dim Action%
On Error GoTo ChildCboxClickERROR
'make sure an item was selected
If mycbox.ListIndex = -1 Then GoTo ChildCboxClickEXIT
'set mousepointer
Screen.MousePointer = 11
'initial settings for fields
Call ClearFormControlsProc(Me)
Call LockFormControlsProc(Me, True)
'set sql
wclause$ = " WHERE [SOCIAL SECURITY NUMBER] = '" & ssn(str$(mycbox.ItemData(m
ycbox.List
Index))) & "';"
'call reader routine
Call Reader(Me, Formsql & wclause$, "")
'special case---
'load photo
Call LoadPhoto
'load combos with relationship information
Call LoadRelationshipsProc
Call SetSelectedNames
'make sure ssn field is not enabled
picTssn.Enabled = False
'set form caption
Me.caption = "Consumer Profile [" & mycbox.text & "]"
'set form tag
' Tag = False
'find the age of this consumer (not saved into database)
findAge
'set form tag 'should be after all other events in module Lou 6/15/05
Tag = False
ChildCboxClickEXIT:
Screen.MousePointer = 0
Exit Sub
ChildCboxClickERROR:
Action = errors(Err)
Select Case Action
Case 0
Resume
Case 1
Resume Next
Case 2, 3
GoTo ChildCboxClickEXIT
End Select
End Sub
Private Sub LoadPhoto()
Dim MYTABLE As Recordset, id$
Dim photoRS As dao.Recordset 'open recordset and set data control to it for compatibility with Access 2000 (jet 4.0) 4/28/05 Lou
Set photoRS = mydb.OpenRecordset("SELECT
* FROM photographs WHERE ssn = '" & ssn(str$(mycbox.ItemData(m
ycbox.List
Index))) & "';")
photoRS.Requery
Set DATA2.Recordset = photoRS
' DATA2.RecordSource = "SELECT * FROM photographs WHERE ssn = '" & ssn(str$(mycbox.ItemData(m
ycbox.List
Index))) & "';"
' DATA2.Refresh
DoEvents
'Make sure there is a photo record
If DATA2.Recordset.RecordCoun
t < 1 Then
'add consumer photo
Set MYTABLE = mydb.OpenRecordset("PHOTOG
RAPHS", dbOpenTable)
MYTABLE.AddNew
MYTABLE!ssn = ssn(str$(mycbox.ItemData(m
ycbox.List
Index)))
MYTABLE.Update
MYTABLE.MoveLast
If MIRRORTRANSACTIONS = True Then
id$ = MYTABLE!ssn
Call RecordTransaction("Photogr
aphs", id$, "A")
End If
MYTABLE.Close
Set photoRS = mydb.OpenRecordset("SELECT
* FROM photographs WHERE ssn = '" & ssn(str$(mycbox.ItemData(m
ycbox.List
Index))) & "';")
photoRS.Requery
Set DATA2.Recordset = photoRS
' DATA2.RecordSource = "SELECT * FROM photographs WHERE ssn = '" & ssn(str$(mycbox.ItemData(m
ycbox.List
Index))) & "';"
' DATA2.Refresh
DoEvents
End If
Picture1.Tag = False
End Sub
Start Free Trial