Results 1 to 13 of 13

Thread: NULL FIELDS!!!

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Posts
    116

    Exclamation

    How can I read a NULL field from ACCESS into
    a control in VB without getting Run Time
    Error 94 (Invalid use of Null).

    Example: Text1 = !Field1
    When Field1 is 'empty' I get Run Time Error 94.

    Help!
    0101011001000010
    01101111011011100110110001101001011011100110010101110010

  2. #2
    Fanatic Member RealisticGraphics's Avatar
    Join Date
    Jul 1999
    Location
    Arkansas
    Posts
    655
    This is where "error handling" comes in handy. Add something like the following to your code.

    Code:
    On Error Goto CheckError
    
    Text1 = !Field1
    
    Exit Sub
    CheckError:
    If Err.Number = 94 Then
      Text1 = "" 'Or whatever you want it to return to the user.
    Else
      Msgbox Err.Number & ":" & vbCrLf & Err.Description, "Error"
    End If
    End Sub
    This should get you out!
    www.RealisticGraphics.net

    Running VS.Net Enterprise & VB 6

    Other Languages: JavaScript, VBScript, VBA, HTML, CSS, ASP, SQL, XML

    MSN Messenger: kmsheff

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Posts
    116
    0101011001000010
    01101111011011100110110001101001011011100110010101110010

  4. #4
    Lively Member
    Join Date
    Aug 2000
    Location
    Bristol, UK
    Posts
    86

    Lightbulb

    My favourite way of getting around this is as follows:
    Text1 = "" & !Field1

    This way you are never assigning a null value.

  5. #5
    Addicted Member
    Join Date
    Sep 2000
    Posts
    138
    Another way:

    If IsNull(!Field1) then Text1.Text=""

  6. #6
    Fanatic Member RealisticGraphics's Avatar
    Join Date
    Jul 1999
    Location
    Arkansas
    Posts
    655
    xmin:
    When I use the "IsNull()" function in this particular area I get the same error. Non of the function that deal with Nulls seem to work when working in conjunction the the access database as we are using it here. Maybe it's my system, who knows.
    www.RealisticGraphics.net

    Running VS.Net Enterprise & VB 6

    Other Languages: JavaScript, VBScript, VBA, HTML, CSS, ASP, SQL, XML

    MSN Messenger: kmsheff

  7. #7
    Addicted Member
    Join Date
    Sep 2000
    Posts
    138
    Hi, RealisticGraphics. For your message, I made a special attempt at the following code and it has worked quite well. Could you show your code?

    Code
    ______________________________________________________
    Option Explicit
    Dim wsCmptXm As Workspace
    Dim dbCmptXm As Database
    Dim rsXmnee As Recordset

    Private Sub Command1_Click()

    'MoveNext Button

    With rsXmnee
    .MoveNext
    If .EOF Then .MoveLast
    Text1.Text = !Tihao ¡®Tihao is a Byte field holding the numbers 0 - 255
    If IsNull(!Defen) Then ¡®Defen is a Single field
    Text2.Text = "Null Field"
    Else
    Text2.Text = !Defen
    End If
    If IsNull(!Beizhu) Then ¡®Beizhu is a Text field
    Text3.Text = "Null Field"
    Else
    Text3.Text = !Beizhu
    End If
    End With

    End Sub

    Private Sub Command2_Click()

    'MovePrevious Button

    With rsXmnee
    .MovePrevious
    If .BOF Then .MoveFirst
    Text1.Text = !Tihao
    If IsNull(!Defen) Then
    Text2.Text = "Null Field"
    Else
    Text2.Text = !Defen
    End If
    If IsNull(!Beizhu) Then
    Text3.Text = "Null Field"
    Else
    Text3.Text = !Beizhu
    End If
    End With

    End Sub

    Private Sub Command3_Click()

    'Close Button

    rsXmnee.Close
    dbCmptXm.Close
    wsCmptXm.Close
    End

    End Sub

    Private Sub Form_Load()

    Set wsCmptXm = DBEngine.Workspaces(0)
    Set dbCmptXm = wsCmptXm.OpenDatabase(App.Path & "\CmptXm.mdb")
    Set rsXmnee = dbCmptXm.OpenRecordset("Xmnee", dbOpenDynaset)
    With rsXmnee
    .MoveFirst
    Text1.Text = !Tihao
    If IsNull(!Defen) Then
    Text2.Text = "Null Field"
    Else
    Text2.Text = !Defen
    End If
    If IsNull(!Beizhu) Then
    Text3.Text = "Null Field"
    Else
    Text3.Text = !Beizhu
    End If
    End With

    End Sub
    _____________________________________________________
    Visual Basic Professional 6.0

    [Edited by xmin on 09-30-2000 at 04:37 AM]

  8. #8
    Fanatic Member RealisticGraphics's Avatar
    Join Date
    Jul 1999
    Location
    Arkansas
    Posts
    655
    Tried using that code (modified a bit) and got the same result. The modified portion is that I made it use a data control as I had previously, so I connected using dao with no data control as you did and it worked fine. It must be something with the data control, I thought... I reinstalled VB and now both ways work. Fluke, I guess.
    www.RealisticGraphics.net

    Running VS.Net Enterprise & VB 6

    Other Languages: JavaScript, VBScript, VBA, HTML, CSS, ASP, SQL, XML

    MSN Messenger: kmsheff

  9. #9
    Addicted Member S@NSIS's Avatar
    Join Date
    Aug 2000
    Location
    Stoke-On-Trent, England
    Posts
    243
    Hi,
    Nothing wrong with xmin's code but you might find that this looks better. (shortens your code too )

    Code:
    'Old code
    'If IsNull(!Defen) 
    'Text2.Text = "Null Field" 
    'Else 
    'Text2.Text = !Defen 
    'End If
    
    'new code
    Text2.Text = IIf(IsNull(!Defen),"Null Field",!Defen)
    Hope this helps

    Shaun
    Web/Application Developer
    VB6 Ent (SP5), Win 2000,SQL Server 2000

  10. #10
    Addicted Member
    Join Date
    Sep 2000
    Posts
    138
    Very good, S@NSIS!

  11. #11
    New Member
    Join Date
    Oct 2000
    Location
    UK
    Posts
    4

    Smile Re: Null Fields

    Hi,

    I find the following method works:
    mycontrol.text = rs(fieldname) & ""

    also,
    try the following to check for nulls and empty strings at the same time:
    if len(rs(fieldname) & "")=0

    hope this helps

  12. #12
    New Member
    Join Date
    Oct 2000
    Posts
    5
    I always use this one:

    If !Name <> "" Then
    txtName = !Name
    End If


    I post this b/c it seems simple, but there were so many other methods posted. Is there a speed consideration to use the If/Then statements when dealing with null values?


  13. #13
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    flewis:
    FYI, testing for a zero-length string ("") is not the same as testing for Null. I tend to use the method posted by S@NSIS:
    Text1.Text = IIf(IsNull(!MyField), "", !MyField)
    "It's cold gin time again ..."

    Check out my website here.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width