Friday 12 October 2018

Acces VBA code combination not working, issue with exit sub?



I'm very new at VBA and I can't seem to solve this problem. It seems like it should be an easy problem to fix, I just don't know how.


So what the code does: In the main form is a record. This record will be copied to another table bij pressing a button on the form. Before the copying begins, the code checks first if the 'OMnummer' has been filled in in the subform. If not, a message box appears saying that the user should fill in the subform and the code stops running.


Secondly the code checks if the record is allready present in the other table. If so, a messagebox appears and the record will not be copied. If not, the record will be copied to the other table.


Both pieces of code work ok seperately. However, when I try to include both pieces in the same private sub, only the first one works. It may be something with the 'Exit Sub' code or I am not using the If - Then right.


Hope you can help me! Monika


Private Sub KnopProjectVersturen_Click()
On Error GoTo ErrProc

If IsNull(Me!Subform_OMnummers.Form!Omnr) Then
If MsgBox("Vul het OMnummer in. Je kan het project niet exporteren zonder OMnummer.") Then
Exit Sub


DoCmd.OpenQuery "Qry_Depo_ControleAanwezig"
If DCount("Deponering.projectnummer", "Qry_Depo_ControleAanwezig") = 0 Then
DoCmd.SetWarnings False
DoCmd.OpenQuery "Qry_projectnaarDepot"
DoCmd.OpenQuery "Qry_ToevoegProjectDepot"
DoCmd.OpenForm "Depot_uitvoer", , , "[Projectnummer] = '" & Me![Projectnummer] & "' And [subID]=[subID]"
Me.Status = 8
DoCmd.Close acQuery, ("Qry_Depo_ControleAanwezig")
DoCmd.SetWarnings True

Else
MsgBox "Dit project bestaat al in de Depot_Uitvoer, verander de status in het projectformulier", vbInformation, "Example"
DoCmd.Close acQuery, ("Qry_Depo_ControleAanwezig")



End If
End If
End If



'Als er fouten zijn laat deze code een messagebox zien met het nummer en de melding.
ExitProc:
Exit Sub
ErrProc:
Select Case Err.Number
Case Else
MsgBox Err.Number & "--" & Err.Description
Resume ExitProc
End Select
End Sub

Answer



There is no condition for that if formula with the message box. Give this a try-


If IsNull(Me!Subform_OMnummers.Form!Omnr) Then
if msgbox("foo",vbAbort,"error") = 3 then
exit sub
end if
end if

Or, don't use the if on the message box line


If IsNull(Me!Subform_OMnummers.Form!Omnr) Then
MsgBox("Vul het OMnummer in. Je kan het project niet exporteren zonder OMnummer.")
Exit Sub
end if

No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...