Visual Basic Error

Visual Basic Error

Anonymous
Not applicable
1,212 Views
6 Replies
Message 1 of 7

Visual Basic Error

Anonymous
Not applicable
I have written the following code to work in VB6.0 and at first everything works fine, after saving and re-opening the file I keep getting this error message:

"User-defined type not defined"

This is the code:

Private Sub TextBoxCatPrijs_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

'Staat alleen de invoer van getallen en een aantal noodzakelijke tekens toe

If KeyAscii = 44 Or KeyAscii = 45 Or KeyAscii = 46 Then Exit Sub Else

If KeyAscii < 48 Or KeyAscii > 57 Then
KeyAscii = 0
Beep
Else
End If

End Sub

There are about 20 textboxes to which I want to add this code. Can anyone help me with this problem?

Thanks allready
0 Likes
1,213 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable
I'm wondering if you wrote that in vba and tried to run it in vb? The syntax is not exactly the same. 'VBA SYNTAX Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) KeyAscii = DecimalOnly(TextBox1, KeyAscii) End Sub 'VB SYNTAX Private Sub TextBox1_Keypress(KeyAscii As Integer) KeyAscii = DecimalOnly(TextBox1, KeyAscii) End Sub If you want to apply it to several textboxes you may want to wrap it in a sub that takes a textbox as an argument so you only have to write it once then pass each textbox to it as arg. eg for examples above Function DecimalOnly(ByRef objCtl As Object, ByVal iIndex As Integer) As Integer Dim rtn As Integer Select Case iIndex Case 8 ' backspace/delete ' always allow this key rtn = iIndex Case 48 To 57 ' Asc("0") to Asc("9") ' allow these characters rtn = iIndex Case Asc(".") 'only allow one decimal point If InStr(objCtl.Text, ".") = 0 Then 'allow decimal rtn = iIndex Else rtn = 0 End If Case Else ' reject these characters rtn = 0 End Select DecimalOnly = rtn End Function hth Mark "SeriouslyDutch" wrote in message news:[email protected]... > I have written the following code to work in VB6.0 and at first everything works fine, after saving and re-opening the file I keep getting this error message: > > "User-defined type not defined" > > This is the code: > > Private Sub TextBoxCatPrijs_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) > > 'Staat alleen de invoer van getallen en een aantal noodzakelijke tekens toe > > If KeyAscii = 44 Or KeyAscii = 45 Or KeyAscii = 46 Then Exit Sub Else > > If KeyAscii < 48 Or KeyAscii > 57 Then > KeyAscii = 0 > Beep > Else > End If > > End Sub > > There are about 20 textboxes to which I want to add this code. Can anyone help me with this problem? > > Thanks allready
0 Likes
Message 3 of 7

Anonymous
Not applicable
There is one more problem, I did write the code in VBA and changed it to work in VB but now when I select the KeyPress option from the menu belonging to the correct textbox in VB I still get the VBA code...how is this possible?
0 Likes
Message 4 of 7

Anonymous
Not applicable
"SeriouslyDutch" wrote in message news:[email protected]... > There is one more problem, I did write the code in VBA and changed it to work in VB but now when I select the KeyPress option from the menu belonging to the correct textbox in VB I still get the VBA code...how is this possible? Sorry, I'm not understanding. What do you mean by "when I select the KeyPress option from the menu belonging to the correct textbox " What menu are you talking about? If you are in vb6, place a textbox on an empty form, double click in the textbox, the code window pops up. If you go to the top right drop down box and select Keypress, a sub is created with this format: Private Sub Text1_KeyPress(KeyAscii As Integer) End Sub that is the syntax you need in vb. Now what do you mean by "menu belonging to the textbox"?
0 Likes
Message 5 of 7

Anonymous
Not applicable
The method you described is exactly what I did. But when I select that drop down menu in VB 6.0 I get the wrong code, the code I should get in VBA (ByVal KeyAscii as MSform....), maybe because the form was originaly designed in VBA...

Is there a way to solve this problem?
0 Likes
Message 6 of 7

Anonymous
Not applicable
SeriouslyDutch wrote: > The method you described is exactly what I did. But when I select that drop down menu in VB 6.0 I get the wrong code, the code I should get in VBA (ByVal KeyAscii as MSform....), maybe because the form was originaly designed in VBA... VBA forms are not direct counterparts to VB forms. One solution is to redesign the form in VB and copy your code across, making adjustments for differrences in types such as the one you've encountered. -- There are 10 kinds of people: those who understand binary and those who don't.
0 Likes
Message 7 of 7

Anonymous
Not applicable
When you select the object and procedure from the drop-down lists at the top of the code pane VB searches for a procedure with the correct name but doesn't check it's signature - that only happens at compile-time. If a procedure with the correct name is not found, an empty one will be created - with the correct signature. You can comment out the incorrect one, select it from the drop-down to recreate it, and move your code into the new procedure.
0 Likes