Forum Discussion
Trouble identifying 'Primary' phone number
- Mar 08, 2025
I discourage using SQL query when you are in a Form and the form is Dirty.
you can use the Form's Recordsetclone.
Dim m_PhoneID As Long Private Sub YesNoField_BeforeUpdate(Cancel As Integer) If Me.YesNoField = -1 Then With Me.RecordsetClone .FindFirst "YesNoField = -1 And CustomerID = " & Me.CustomerID & " And PhoneID <> " & Me.PhoneID If Not .NoMatch Then If MsgBox("Do you want to make this PhoneNumber the primary phone number?", vbQuestion + vbYesNo) = vbYes Then m_PhoneID = !PhoneID Me.TimerInterval = 100 Else Me.YesNoField.Undo Cancel = True End If End If End With End If End Sub Private Sub Form_Timer() ' kill the timer Me.TimerInterval = 0 With Me.RecordsetClone .FindFirst "PhoneID = " & m_PhoneID .Edit !YesNoField = 0 .Update End With End Sub
You're not showing the design of the table, so I can freely speculate you have a CustomerID field as well, and are capturing the several phone numbers for each customer.
If so, in Form_AfterUpdate you can count the number of records with Primary field set to True, using DCount, and if greater than 1, run an Update query to set all but the current record to False. Something like:
(off the cuff)
dim sql as string
if me.Primary=True then
if dcount("*", "tblPhoneNumbers", "CustomerID=" & Me.CustomerID) > 1 then
sql="update tblPhoneNumbers set Primary=false where CustomerID=" & me.CustomerID & " and PhoneID <> " & Me.PhoneID
Currentdb.Execute sql, dbFailOnError
endif
endif
- ns61Mar 10, 2025Copper Contributor
Tom_van_Stiphoutthanks for your solution, and yes, your speculations are correct. I actually was able to resolve this by
However, I'm open to seeing other approaches as well so I may play around with yours too