Compile Error..

Nielinki

New Member
Joined
Mar 3, 2025
Messages
18
Office Version
  1. 365
Platform
  1. Windows
Hi there (VBA beginner)
This happens when I try to run the Enter Payment button..
What am I doing wrong please?

Screenshot 2025-03-06 112505.png
Screenshot 2025-03-06 112322.png
Screenshot 2025-03-06 112446.png


VBA Code:
Option Explicit

Sub Order_EnterPayment()
Dim PayType As String
Dim PmntType As String

If PmntType = "Cash" Then 'Cash
    PmntFrm.PayType.Value = "Cash"
    PmntFrm.Cash.BackColor = RGB(100, 43, 75) 
    PmntFrm.Card.BackColor = RGB(0, 0, 0)
Else 'Credit Card
    PmntFrm.PayType.Value = "Card"
    PmntFrm.Cash.BackColor = RGB(0, 0, 0) 
    PmntFrm.Card.BackColor = RGB(100, 43, 75)
End If

PmntFrm.PmntAmnt.Value = POS.Range("B13").Value
PmntFrm.Show
End Sub

VBA Code:
Option Explicit
Dim BtnNumb As Long
Private Sub Button0_Click()
BtnNumb = 0
UpdatePmntAmnt BtnNumb
End Sub

Private Sub Button1_Click()
BtnNumb = 1
UpdatePmntAmnt BtnNumb
End Sub

Private Sub Button2_Click()
BtnNumb = 2
UpdatePmntAmnt BtnNumb
End Sub

Private Sub Button3_Click()
BtnNumb = 3
UpdatePmntAmnt BtnNumb
End Sub

Private Sub Button4_Click()
BtnNumb = 4
UpdatePmntAmnt BtnNumb
End Sub

Private Sub Button5_Click()
BtnNumb = 5
UpdatePmntAmnt BtnNumb
End Sub

Private Sub Button6_Click()
BtnNumb = 6
UpdatePmntAmnt BtnNumb
End Sub

Private Sub Button7_Click()
BtnNumb = 7
UpdatePmntAmnt BtnNumb
End Sub

Private Sub Button8_Click()
BtnNumb = 8
UpdatePmntAmnt BtnNumb
End Sub

Private Sub Button9_Click()
BtnNumb = 9
UpdatePmntAmnt BtnNumb
End Sub

Private Sub CancelBtn_Click()
Unload Me
End Sub

Private Sub Clear_Click()
Me.PmntAmnt.Value = "" 'Clear Payment
End Sub

Private Sub DecPoint_Click()
If InStr(Me.PmntAmnt.Value, ".") = 0 Then Me.PmntAmnt.Value = Me.PmntAmnt.Value & "."
End Sub

Sub UpdatePmntAmnt(BtnNumb As Long)
PmntFrm.PmntAmnt.Value = PmntFrm.PmntAmnt.Value & BtnNumb
End Sub

Public Sub Enter_Payment()
Dim PmntType As String
    PmntFrm.PmntAmnt.Value = POS.Range("B13").Value
    PmntFrm.Show
End Sub


Public Sub Card_Click()
Dim PayType As String
    PmntFrm.PayType.Value = "Card"
    PmntFrm.Cash.BackColor = RGB(0, 0, 0)
    PmntFrm.Card.BackColor = RGB(100, 43, 75)
End Sub

Public Sub Cash_Click()
Dim PayType As String
    PmntFrm.PayType.Value = "Cash"
    PmntFrm.Cash.BackColor = RGB(100, 43, 75)
    PmntFrm.Card.BackColor = RGB(0, 0, 0)
End Sub

Sub EnterPmntBtn_Click()
With PmntFrm
    If IsNumeric(.PmntAmnt.Value) = False Then
        MsgBox "Please make sure to enter a correct Payment Amount"
        .PmntAmnt.Value = ""
        .PmntAmnt.SetFocus
        Exit Sub
    End If
    If .PmntAmnt.Value < POS.Range("V27").Value Then
        If MsgBox("The Payment Amount is less than the total bill of " & POS.Range("V27").Value & vbCrLf & "Are you sure you want to proceed", vbYesNo, "Payment Amount Issue") = vbNo Then Exit Sub
    End If
    POS.Range("B12").Value = .PayType.Value 'Hidden Field on form
    POS.Range("B13").Value = .PmntAmnt.Value 'Set Payment Amount
    POS.Range("V28").Value = .PmntAmnt.Value 'Set TotalRange Payment Amount
    Unload PmntFrm
End With
End Sub
 
Hello @Nielinki.
This means that you don't have a textbox field named PayType on your form (PmntFRM). Add a textbox field named PayType to the form and set Visible = False in the properties of this object (PayType).
Compile Error_PayType.png
Compile Error_PayType_Visible.png

And you'll be happy.
 
Upvote 0

Attachments

  • Screenshot 2025-03-06 125012.png
    Screenshot 2025-03-06 125012.png
    60.9 KB · Views: 6
Upvote 0
You changed the code, return that code and it will work correctly. You added something again that was not in the original code!
 
Upvote 0
VBA Code:
Option Explicit
Dim BtnNumb         As Long

Private Sub Button0_Click()
    BtnNumb = 0
    UpdatePmntAmnt BtnNumb
End Sub

Private Sub Button1_Click()
    BtnNumb = 1
    UpdatePmntAmnt BtnNumb
End Sub

Private Sub Button2_Click()
    BtnNumb = 2
    UpdatePmntAmnt BtnNumb
End Sub

Private Sub Button3_Click()
    BtnNumb = 3
    UpdatePmntAmnt BtnNumb
End Sub

Private Sub Button4_Click()
    BtnNumb = 4
    UpdatePmntAmnt BtnNumb
End Sub

Private Sub Button5_Click()
    BtnNumb = 5
    UpdatePmntAmnt BtnNumb
End Sub

Private Sub Button6_Click()
    BtnNumb = 6
    UpdatePmntAmnt BtnNumb
End Sub

Private Sub Button7_Click()
    BtnNumb = 7
    UpdatePmntAmnt BtnNumb
End Sub

Private Sub Button8_Click()
    BtnNumb = 8
    UpdatePmntAmnt BtnNumb
End Sub

Private Sub Button9_Click()
    BtnNumb = 9
    UpdatePmntAmnt BtnNumb
End Sub

Private Sub CancelBtn_Click()
    Unload Me
End Sub

' Clear Payment
Private Sub Clear_Click()
    Me.PmntAmnt.Value = ""
End Sub

Private Sub DecPoint_Click()
    Dim decSep      As String
    decSep = Application.DecimalSeparator

    If InStr(Me.PmntAmnt.Value, decSep) = 0 Then
        Me.PmntAmnt.Value = Me.PmntAmnt.Value & decSep
    End If

End Sub

Sub UpdatePmntAmnt(BtnNumb As Long)
    PmntFRM.PmntAmnt.Value = PmntFRM.PmntAmnt.Value & BtnNumb
End Sub

Public Sub Enter_Payment()
    Dim PmntType    As String
    PmntFRM.PmntAmnt.Value = POS.Range("B13").Value
    PmntFRM.Show
End Sub

Public Sub Card_Click()
    Dim PayType     As String
    PmntFRM.PayType.Value = "Card"
    PmntFRM.Cash.BackColor = RGB(0, 0, 0)
    PmntFRM.Card.BackColor = RGB(100, 43, 75)
End Sub

Public Sub Cash_Click()
    Dim PayType     As String
    PmntFRM.PayType.Value = "Cash"
    PmntFRM.Cash.BackColor = RGB(100, 43, 75)
    PmntFRM.Card.BackColor = RGB(0, 0, 0)
End Sub

Sub EnterPmntBtn_Click()
    Dim paymentAmount As Double

    Dim decSep      As String
    decSep = Application.DecimalSeparator

    With PmntFRM

        Dim inputValue As String
        inputValue = Replace(.PmntAmnt.Value, IIf(decSep = ",", ".", ","), decSep)

        If Not IsNumeric(inputValue) Then
            MsgBox "Please make sure to enter a correct Payment Amount", vbExclamation, "Invalid Input"
            .PmntAmnt.Value = ""
            .PmntAmnt.SetFocus
            Exit Sub
        End If

        paymentAmount = CDbl(inputValue)

        If paymentAmount < POS.Range("V27").Value Then

            If MsgBox("The Payment Amount is less than the total bill of " & POS.Range("V27").Value & vbCrLf & "Are you sure you want to proceed?", vbYesNo, "Payment Amount Issue") = vbNo Then
                Exit Sub
            End If

        End If

        POS.Range("B12").Value = .PayType.Value     ' Hidden Field on form
        POS.Range("B13").Value = paymentAmount      ' Set Payment Amount
        POS.Range("V28").Value = paymentAmount      ' Set TotalRange Payment Amount
        Unload PmntFRM
    End With

End Sub
And
VBA Code:
Option Explicit

Sub Order_EnterPayment()
    Dim PayType     As String
    Dim PmntType    As String

    ' Cash
    If PmntType = "Cash" Then
        PmntFRM.PayType.Value = "Cash"
        PmntFRM.Cash.BackColor = RGB(100, 43, 75)
        PmntFRM.Card.BackColor = RGB(0, 0, 0)

        ' Credit Card
    Else
        PmntFRM.PayType.Value = "Card"
        PmntFRM.Cash.BackColor = RGB(0, 0, 0)
        PmntFRM.Card.BackColor = RGB(100, 43, 75)
    End If

    PmntFRM.PmntAmnt.Value = POS.Range("B13").Value
    PmntFRM.Show
End Sub
This code works for me without a single error. Yes, I made some minor changes, but still.
Compile Error_PayType_After.png
 
Last edited:
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top