Search code examples
vbams-accessms-access-2016

Syntax error in my If Else statement in my VBA access code


I have been working on this problem for a while and I'm close to the answer thanks to the help of stack overflow and google. Let me give you some insight. I have a table and 2 forms and a Key named CalibrationKey that auto increments as a Autonumber to keep track of the record and how many and what not. On 1 form are all my table fields. On my 2nd form is the Subfields to the first form that feed into the table. First form is Calibration and 2nd form is Calibration Detail. In my first form, I have it so you can edit OR add a new record(where my problem started). My 2nd form I have it so you can only add a new record and have the Data Entry on Property Sheet to YES as I only want you to be able to add a new record from that form. So I made a If Else statement as a click event in my First Form that if my Text box is Empty, then you add a new record. If it is NOT empty, then you edit the form. But, I get a Syntax error every time I try and do this. I'm not sure what the problem is.

Private Sub Make_Click()
If Not IsNull(Make.Value) Then
DoCmd.OpenForm "Calibration Detail", , , "[CalibrationKey]= " & Me.[CalibrationKey], acFormAdd, acDialog
Else: DoCmd.OpenForm "Calibration Detail", , , "[CalibrationKey]= " & Me.[CalibrationKey], acFormEdit, acDialog
End If
End Sub

Then I found that a IsEmpty might be easier, but it gives the same Syntax error in the ELSE line of code as the Not IsNull line did. 

Private Sub Make_Click()
If IsEmpty(Make.Value) Then
DoCmd.OpenForm "Calibration Detail", , , "[CalibrationKey]= " & Me.[CalibrationKey], acFormAdd, acDialog
Else: DoCmd.OpenForm "Calibration Detail", , , "[CalibrationKey]= " & Me.[CalibrationKey], acFormEdit, acDialog
End If
End Sub 

Trying to diagnose the problem of the incorrect Syntax as Either IsEmpty or Not IsNull seems to give the same syntax error on the same line. Looking to correct the syntax error and which is function is better to use, rather it be IsEmpty, or Not IsNull


Solution

  • Try using the normal If-Then-Else syntax:

    Private Sub Make_Click()
    
        Dim DataMode As acFormOpenDataMode
    
        If Me.Dirty = True Then
            'Save record.
            Me.Dirty = False
        End If
    
        If Not IsNull(Make.Value) Then
            DataMode = acFormOpenDataMode.acFormAdd
        Else
            DataMode = acFormOpenDataMode.acFormEdit
        End If
        DoCmd.OpenForm "Calibration Detail", , , "[CalibrationKey]= " & Me.[CalibrationKey], DataMode, acDialog
    
    End Sub