Compile Error "Expected ="

GBalcom

Much to learn!
Local time
Today, 05:30
Joined
Jun 7, 2012
Messages
460
Hello everyone,
It's been a year and a half since I've done any programming, and it's amazing what I've forgotten. I spend yesterday remembering how to do cascading combo boxes, and todays challenge is this piece of code.

Code:
Public Function WidthCalc(WidthInput As Double, SubtypeID As Integer)
'declare variables
Dim min As Integer
Dim max As Integer
Dim x As Double
Dim intWidthInput As Integer

'convert WidthInput to integer for Select Case
    intWidthInput = CInt(WidthInput)
    Debug.Print intWidthInput
    

'Ensure that the value is between the min and max values for that cabinet subtype

min = 1 'FindMinWidth(SubtypeID)
max = 30 'FindMaxWidth(SubtypeID)

Select Case intWidthInput
    Case Is < min
        MsgBox "Value entered is too small for selected cabinet type.  Please try again."
        
        
    Case Is > max
        MsgBox "Value entered is too large for selected cabinet typ.  Please try again."
       
End Select

'Populate variable
    x = WidthInput / 3
    x = Round(x, 0)
    x = x + 3
    
    Debug.Print x
    

End Function

I receive the error in the title when I try and run this...It won't let me step through....

I've tried breaking down the code to ascertain what is causing it. Even brought it down to here:

Code:
Public Function WidthCalc2(WidthInput As Double, SubtypeID As Integer) As Integer
'declare variables

WidthCalc2 = 1

    MsgBox WidthCalc2
    

End Function

But it still errors out. I'm sure it must be something stupid, and I'll be embarrassed that I put this out there, but I need to understand what's happening...


PS. The two other functions it's calling works fine...
Thanks everyone!
 
You cannot

MsgBox WidthCalc2

because the latter is a function expecting two parameters and you are calling it in that statement. (Despite the fact that mere assigning a value to it is actually done by WIdthCal2= someValue - but here tyhe function name is on the left-hande side of the equal sign , so it receives a value)

You want to check the value, store it in some other variable and then assign it to the function at the end.
 
It won't let you step through at all?

Can I suggest adding Option Explicit at the top of the module and trying Debug > Compile... to see if it picks up any variable issues?
 
Or, spike's answer... :rolleyes:
 
Thank you both, it is much appreciated...I did add the "Option explicit" to all my codes, and turned it on in the options....

However, I'm still having the same issue with my original code, and I don't see where I'm messageboxing anything...
 
Here's my latest code (still not working)

Code:
Public Function WidthCalc(WidthInput As Double, SubtypeID As Integer)
'declare variables
Dim min As Integer
Dim max As Integer
Dim x As Double

'Ensure that the value is between the min and max values for that cabinet subtype

min = FindMinWidth(SubtypeID)
max = FindMaxWidth(SubtypeID)

Select Case Int(WidthInput)
    Case Is < min
        MsgBox "Value entered is too small for selected cabinet type.  Please try again."
        
        
    Case Is > max
        MsgBox "Value entered is too large for selected cabinet type.  Please try again."
       
End Select

'Populate variable
    x = WidthInput / 3
    x = Int(x)
    x = x * 3
    x = x + 3
    
    Debug.Print x
    
    WidthCalc = x
    

End Function
 
This is a rather silly game. We debug one code - the one you submitted - but you seem to work on some other problems. Please make up your mind what the problems are (and btw: "not working" is the most meaningless phrase in the universe - read my signature).
 
Spike,
my apologies. The problem still remains the same as it was in the first version of code I posted. I was trying to clean up the code, and realized I didn't need as many variables. I just realized the problem has something to do with the second variable in the function heading:

Public Function WidthCalc(WidthInput As Double, SubtypeID As Integer)

IF I remove this, the error goes away. Not sure why though.
 
Some comments of my own...

Code:
Public Function WidthCalc(WidthInput As Double, SubtypeID As Integer)

What type of value are you expecting the function to return? Is it an integer (long or otherwise) or a double? You need to define this (based on the fact you assign WidthCalc the value of x, which itself is defined as a Double, that is what I would assume it should be?...)

Code:
Public Function WidthCalc(WidthInput As Double, SubtypeID As Integer) [COLOR=red]As Double[/COLOR]

----

Code:
Select Case Int(WidthInput)
  Case Is < min
    MsgBox "Value entered is too small for selected cabinet type.  Please try again."
  Case Is > max
    MsgBox "Value entered is too large for selected cabinet type.  Please try again."
End Select

You have a check to see if your passed argument WidthInput is outside a defined range. But you don't have a handle for when it breaches that range? You prompt with a message box but you don't tell the function to stop in either scenario, so it still continues on, performs the calculation and returns it?

Code:
Select Case Int(WidthInput)    [COLOR=red]' Why not CInt?... And why convert to an integer at all? Potential issues with rounding?[/COLOR]
 
  Case Is < min
    MsgBox "Value entered is too small for selected cabinet type.  Please try again."
    [COLOR=red]End    ' Or some appropriate handler a la Exit Function or whatever[/COLOR]
 
  Case Is > max
    MsgBox "Value entered is too large for selected cabinet type.  Please try again."
    [COLOR=red]End    ' Or some appropriate handler a la Exit Function or whatever[/COLOR]
 
[COLOR=red]  Case Else[/COLOR]
 
End Select

----

What happens when you debug / step-through your code? When you say 'not working' - to spike's point, this doesn't really tell us anything. What exactly isn't working? Are you getting an error? What error? On what line? Are you setting watches on all your variables and tracking their values line-by-line to see where they are changing or to validate that they are holding the values you expect them to?
 
Where are you calling WidthCalc?
 
AOB,
Thank you for your time to respond. I appreciate it very much. The value I'm expecting to be returned is an integer. I will correct this. I'm using "Int" instead of "Cint" because I don't want it to round. I need just the integer itself. Thank you for the case else. I'll add in proper error routines later, I'm just trying to conceptualize this at the moment.

I will be calling WidthCalc from a button on a form. The function resides in a module.

What I meant earlier was that I cannot step through the code, it simply gives me that error (Compiled Error, expected = ). My other functions I can step through/debug just fine.
 
You've got me thinking....I was using the "Cint" to convert to an integer because I didn't think it would work if it wasn't....I'd actually prefer to not convert it...I'll try it and see..
 
My point is, when you call WidthCalc, are you passing appropriate variables for it to return a result?

If you're calling WidthCalc from a button on a form, presumably you are using an OnClick event for that button? What does that look like? Where does it get the values for the parameters you need to pass and what does it do with the returned value?

Code:
Private Sub comYourButton_Click()
 
    Me.txtYourTextBox.Value = WidthCalc([COLOR=red]dblParameter1[/COLOR], [COLOR=red]intParameter2[/COLOR])  ' ??
 
End Sub

Where are you getting those parameters from and are they definitely of the correct type?

You can't debug a function that requires parameters because you've no way of passing values to it. You can only debug it by calling it from somewhere else and provide the values (i.e. your command button, or a test sub/function somewhere)
 
AOB,
I haven't wrote the onClick for the button yet...I've been debugging in the immediate window. Yes, it will pass a value from one of the forms text boxes to the function to use.
 
Show me how you call the WidthCalc function in the Immediate window then
 
Personally, I would advise against using the Immediate window for debugging functions like this - it is exempt from the Option Explicit statement and therefore won't help you identify issues resulting from variable declaration / scope inaccuracies.

Instead, try just creating a test sub/function, from which you call your WidthCalc function :

Code:
Option Explicit
 
Public Sub TestWidthCalc
 
  Dim x As Double
  Dim dblParameter1 As Double
  Dim intParameter2 As Integer
 
  dblParameter1 = <Appropriate Test Value>
  intParameter2 = <Appropriate Test Value>
 
  x = WidthCalc(dblParameter1, intParameter2)
 
End Function
 
Public Function WidthCalc(WidthInput As Double, SubtypeID As Integer) [COLOR=black]As Double[/COLOR]
[COLOR=black]...[/COLOR]
[COLOR=black]End Function[/COLOR]

Now step through the test function and let it pass values to the function you really want to check. When you're satisfied that WidthCalc is working, you can just dump the test routine (or modify it for your OnClick event)
 

Users who are viewing this thread

Back
Top Bottom