Page 1 of 3 123 LastLast
Results 1 to 15 of 40
  1. #1
    twgonder is offline Expert
    Windows 10 Access 2016
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658

    Number seen as string from form when using in VBA

    I have form with a textbox MyNumber that has as its source a field from MyTable1 called MyNumber that is a byte type (integer less than 255 and >= 0)) in the table.

    Deep down in the code I'm getting
    "12" > 98 = true
    Up above in the code, entry of 12 for the textbox for this statement is showing a data type of 8 (string) in this:

    ?vartype(aFrm.ActiveControl.Text) (aFrm is the form object Me passed to a sub)
    8
    ?fldnm


    MyNumber

    Shouldn't, since the data source is MyNumber (byte), VBA treat aFrm.ActiveControl.Text as numeric and not a string?
    Is there something to set in the form (other than data source) to tell VBA that MyNumber is a number and not a string?
    If I need to convert the string back to a number in the code, is there a property that I can use for the textbox to know it's a number?

    I got the idea to use *.text from another thread. Is it better to use *.value and what's the difference other than maybe converting a number to string?
    Can I use *.value for numbers, dates and strings?

    While I'm at it, I think I saw somewhere a function that returns the data type in English, instead of numbers. Anyone got a link for that (my Google search turned up nothing)?

  2. #2
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Here is a thread that should provide some clues:
    https://stackoverflow.com/questions/...-in-vba-access
    As for the requested function to provide data types names here is one version from Orange (Jack) on this forum, sorry Jack, hope it's OK:
    Code:
    '--------------------------------------------------------------------------------------------------------------' Procedure : FieldType
    ' Author 1  : Jack
    ' Created   : 03/18/2021
    ' Modified  : 03/23/2021
    ' Purpose   : To identify fieldtypes in Access
    '--------------------------------------------------------------------------------------------------------------
    
    
    Private Function FieldType(v_fldtype As Integer) As String
    
    
        On Error GoTo Error_FieldType
    
    
        Select Case v_fldtype
            Case dbDecimal
                FieldType = "Decimal"
            Case dbSingle
                FieldType = "Single"
            Case dbAttachment
                FieldType = "Attachment"
            Case dbBigInt
                FieldType = "BigInteger"
            Case dbBoolean
                FieldType = "Boolean"
            Case dbByte
                FieldType = "Byte"
            Case dbInteger
                FieldType = "Integer"
            Case dbLong
                FieldType = "Long"
            Case dbCurrency
                FieldType = "Currency"
            Case dbSingle
                FieldType = "Single"
            Case dbDouble
                FieldType = "Double"
            Case dbDate
                FieldType = "Date"
            Case dbText
                FieldType = "Text"
            Case dbLongBinary
                FieldType = "LongBinary"
            Case dbMemo
                FieldType = "Memo"
            Case dbGUID
                FieldType = "GUID"
            Case Else
                FieldType = "??Needs definition"    'Not all field types may have been defined
        End Select
    
    
    Exit_Error_Fieldtype:
        Exit Function
    
    
    Error_FieldType:
        MsgBox Err.Number & ": " & Err.Description
        Resume Exit_Error_Fieldtype
    
    
    End Function
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  3. #3
    twgonder is offline Expert
    Windows 10 Access 2016
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658
    Thanks for the routine and link.

    I think the procedure has some errors. Or it's meant for something other than Access VBA.
    The documentation I read (VarType Function - Microsoft Support) says that the variable is vbString for 8 but dbDate = 8.
    You get Date for something that is a string.

  4. #4
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    More reading :
    https://learn.microsoft.com/en-us/of...e-property-dao
    What you linked is not the same as the table field data type as (I think) requested in the original post.

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  5. #5
    twgonder is offline Expert
    Windows 10 Access 2016
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658

    Still a problem with strange numeric comparison

    Quote Originally Posted by Gicu View Post
    More reading :
    https://learn.microsoft.com/en-us/of...e-property-dao
    What you linked is not the same as the table field data type as (I think) requested in the original post.

    Cheers,
    I read the link, I don't think it deals with forms and variables in the form.
    Here is the problem I'm having:

    Click image for larger version. 

Name:	221226BadCompare1.jpg 
Views:	29 
Size:	65.7 KB 
ID:	49370

    .value doesn't work because it doesn't return what the user entered in the textbox.
    So, I'm using .text

    Above is showing the strange result where 12 is greater than 98.
    I'm guessing it's because 12 is seen as a string, but that's not certain given the last line in the immediate window.
    Is there a better way to do this if statement to get the correct result?
    Sometimes the validation procedure will be on strings or dates too.

  6. #6
    twgonder is offline Expert
    Windows 10 Access 2016
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658

    I must be crazy

    I've got to admit, this one really has me stumped:

    Click image for larger version. 

Name:	221226BadCompare2.jpg 
Views:	30 
Size:	48.0 KB 
ID:	49373

  7. #7
    twgonder is offline Expert
    Windows 10 Access 2016
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658

    Is there a clue here?

    Does the space in front of the 98 and not the 12 give a clue as to what's going on?

    Click image for larger version. 

Name:	221226BadCompare3.jpg 
Views:	29 
Size:	71.1 KB 
ID:	49375

  8. #8
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Can you upload a sample with the form in question and the full code. You seem to want to validate the entered value "as you type"; I usually do it after everything is entered and just refer to the control name (as Value is the default property). Where do you declare the array and where do you populate it?

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  9. #9
    twgonder is offline Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658
    Quote Originally Posted by Gicu View Post
    As for the requested function to provide data types names here is one version from Orange (Jack) on this forum, sorry Jack, hope it's OK:
    I've now come back to this post.
    I'm in the Form_Error of a form,
    I can get the activecontrol.name
    but activecontrol.type throws an error

    Is there a way to find out the field's type while in a form's VBA procedure?
    (No help on this from Google)

  10. #10
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    clue is in your question - you want the field type, not the control type so perhaps something like

    Recordset(screen.activecontrol.ControlSource).Type

    here is a list of field types

    https://learn.microsoft.com/en-us/of...e-property-dao

  11. #11
    twgonder is offline Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658
    Dang, lost my message again!
    @cj_l Thank you! I'm still sorting out the control from the data and what is which, where and when.
    I wouldn't have tried the screen and controlsource, so that got me somewhere.

  12. #12
    twgonder is offline Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658

    Back to this one, with crashing Access

    Quote Originally Posted by CJ_London View Post
    clue is in your question - you want the field type, not the control type so perhaps something like

    Recordset(screen.activecontrol.ControlSource).Type
    ...
    So, I added this line to my code in Form_Error:
    ' If Recordset(Screen.ActiveControl.ControlSource).Type = 8 Then 'a date field, look for "."


    (still searching what the "screen." part means,
    but after this line runs, and the next instance of an entry error occurs caused by clicking on another control, Access with some reliability will crash.

    In my sample, the lines after the then don't run, it goes straight to the end if.
    I can stop the crashing by commenting out the line, and it comes right back after uncommenting.
    I'm guessing that using = 8 instead of = dbDate shouldn't cause a problem (I will test while waiting to see if anyone has an idea as to the crashing).

    This is the kind of crash I've been experiencing for months, possibly by some similar line of code.
    (I'll have to debug more, now that I know the kind of thing I'm looking for)

    This line of code does what I asked for in a previous post, is there any reason why it would crash Access?

  13. #13
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    Can’t work with one line of code and a confusing description of what then happens. What does crash mean? You get an error, it freezes? It closes. What do you have in the way of error handling? Are you using option explicit? The list goes on

    re screen, plenty of links out there
    https://learn.microsoft.com/en-us/of.../Access.Screen

  14. #14
    twgonder is offline Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658
    [QUOTE=CJ_London;505975]Can’t work with one line of code and a confusing description of what then happens...
    /QUOTE]

    First, we can start with the syntax. It compiles, but maybe isn't appropriate in the Form_Error procedure?

  15. #15
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    Sorry don’t have time to speculate. Provide proper information or don’t bother. syntax errors typically show as red when you are typing it and complete a line. Compile errors (assuming you are using option explicit) relate to early bound objects - so no error means not a problem there. Run time errors relate to late bound objects and data

Page 1 of 3 123 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Number Wildcards in String
    By pjordan@drcog.org in forum Queries
    Replies: 11
    Last Post: 10-25-2018, 02:01 PM
  2. Replies: 6
    Last Post: 01-18-2016, 02:59 PM
  3. Extract number from String
    By DOSRoss in forum Programming
    Replies: 26
    Last Post: 04-28-2015, 06:07 AM
  4. Replies: 0
    Last Post: 10-22-2012, 02:45 PM
  5. How to Convert string to Number?
    By taimysho0 in forum Programming
    Replies: 1
    Last Post: 02-24-2012, 01:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums