saving wrong record when subform is blank

S

Seren

I have a form with a subform that I've been working on for a while. If the
Tool Number in the subform is left blank, the record for the job (main form)
is saved with the first entry in the table the subform is linked to. I would
rather have a message box pop up alerting the user they must enter a tool
number and then set focus in the tool number field on the subform. I am not
really sure where to put this or if that would solve my problem. (It sounds
like it will... but that doesn't always mean anything.)

Here's the code I have for now:

If IsNull(ToolNum) Then
MsgBox ("You must enter a tool number to save this record.")
ToolNum.SetFocus
End If

I have this at the beginning of my Add_Click sub, but that does not seem to
be what's working for me. any suggestions?

Thanks a bunch!
seren
 
T

Toco

Seren: try this

If Trim(toolcontrol.text) = "" Then
MsgBox ("You must enter a tool number to save this record.")
toolcontrol.SetFocus
Else
....do add stuff

-Toco-
 
S

Seren

well, this is what I have right now... so would it replace the "If
DCount("ToolNum", "tblTool", stDup) > 0 Then" line?

Private Sub ToolNum_AfterUpdate()
Dim TNum As String
Dim stDup As String
Dim ToolID As Integer
Dim rsc As dao.Recordset

'Set rsc = Form_Form2.subTool.Form.RecordsetClone

TNum = Form_Form2.subTool.Form.ToolNum.Value
stDup = "ToolNum = " & "'" & TNum & "'"

' check if toolnum already exists in tblTool
If DCount("ToolNum", "tblTool", stDup) > 0 Then
MsgBox ("corresponding tool number was found")
' clear toolNum field
Form_Form2.subTool.Form.Undo
' set form to existing matching record
ToolID = DLookup("[TOOLID]", "tblTool", "ToolNum ='" & TNum & "'")

Form_Form2.ToolID = ToolID
Form_Form2.subTool.Form.RecordSource = "Select * from tblTool where
ToolNum = '" & TNum & "'"
Form_Form2.subTool.Form.Requery

'rsc.FindFirst stDup
' DoCmd.FindRecord TNum
' DoCmd.GoToRecord acTable, "subTool", acGoTo, TNum
'Form_Form2.subTool.Form.Bookmark = rsc.Bookmark
' MsgBox ("you were taken to the corresponding tool number- Tool
Number: " & ToolNum & _
", Boards Per Panel: " & BdsPerPanel)
End If
If IsNull(ToolNum) Then
MsgBox ("You must enter a tool number to save this record.")
ToolNum.SetFocus
End If
'Set rsc = Nothing
DoCmd.Save
End Sub


I have this directly in the subform code.

Thanks!
Seren
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top