0

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
3
  • The code after Exit Sub is never executed, check where you put End If-statements!
    – AKDADEVIL
    Commented Jul 24, 2013 at 10:12
  • Thanks AKDADEVIL for your reply! I'll read up on the placements of the End If statements and improve my codes accordingly. Didn't realise that placement was an issue.
    – user2613982
    Commented Jul 24, 2013 at 10:42
  • use this for code indenting - it will show you the levels and make matching if/endif and other like constructs easier. It works in Office 2007 and 2010, but the website does not show those
    – SeanC
    Commented Jul 24, 2013 at 15:08

2 Answers 2

0

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
2
  • Dear All, Thank you so much for your replies and I am very sorry for the crossposting. I didn't realise the two fora were connected. Raystafarian, thanks for your code. I tried it, but it didn't work. Did work without the If in the messagebox line: Matzone fixed the code for me where I crossposted: stackoverflow.com/questions/17830433/…. So thank you all! Saved my day :) I was getting really grumpy.
    – Monika
    Commented Jul 24, 2013 at 10:45
  • @Monika you'd need two end if in the first code snippet. you need to close them both, I just didn't include the first one there, so I didn't close it. I edited to include it, in case you want to prompt the user. Commented Jul 24, 2013 at 10:58
1

Not so sure .. but you may try this ..

Private Sub KnopProjectVersturen_Click()
On Error GoTo ErrProc


'This is the first part of the code checking if the subform has been filled in

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    


'This is the second part of the code, checking if the record exists in the other table and then copying part of the record

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    


'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
2
  • Dear Matzone, thank you that works like a dream! So if I understand correctly, the problem lies in the If statement with the first messagebox?
    – user2613982
    Commented Jul 24, 2013 at 10:41
  • @user2613982 .. yes, it's in your IF block .. So, don't forget make a green tick below the downvote sign .. :)
    – matzone
    Commented Jul 24, 2013 at 10:44

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.