Visual Basic Error

Discussion in 'AutoCAD' started by SeriouslyDutch, Feb 1, 2005.

  1. 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
     
    SeriouslyDutch, Feb 1, 2005
    #1
  2. SeriouslyDutch

    MP Guest

    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

    works fine, after saving and re-opening the file I keep getting this error
    message:
     
    MP, Feb 1, 2005
    #2
  3. 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?
     
    SeriouslyDutch, Feb 1, 2005
    #3
  4. SeriouslyDutch

    MP Guest

    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"?
     
    MP, Feb 1, 2005
    #4
  5. 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?
     
    SeriouslyDutch, Feb 2, 2005
    #5
  6. 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.
     
    Frank Oquendo, Feb 2, 2005
    #6
  7. SeriouslyDutch

    fantum Guest

    When you select the object and procedure from the drop-down lists and 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.
     
    fantum, Feb 2, 2005
    #7
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.