Results 1 to 5 of 5
  1. #1
    Fish218 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    68

    Dim var@@@ As Integer Question...

    So I have the following two sets of the same code. The only difference between the two is highlighted in red. Why does the top code work and the bottom doesn't?? The only difference is removing "As Integer" to get the bottom code to work. Isn't varFishCount an integer in the query?

    Private Sub Length_AfterUpdate()
    Dim varYear As Integer
    Dim varAssessment As Integer
    Dim varCollector As Integer
    Dim varFishCount



    varYear = [Forms]![frmCommercial2]![Form1]![Text422]
    varAssessment = [Forms]![frmCommercial2]![Form1]![Text444]
    varCollector = [Forms]![frmCommercial2]![Form1]![Combo431]
    varFishCount = DLookup("[CountofIndividualFishID]", "qry2012", "[CollectionYear]=" & varYear & " And [CollectorID]=" & varCollector & " And [AssessCodeID]=" & varAssessment)

    If (IsNull(varFishCount)) Then
    [Text46] = 0
    [Text48] = 1
    ElseIf Not (IsNull(varFishCount)) Then
    [Text46] = varFishCount
    [Text48] = varFishCount + 1
    End If
    [FishNum] = [Forms]![frmCommercial2]![Form1]![Text422] & "-" & [Forms]![frmCommercial2]![Form1]![Text444] & "-" & [Forms]![frmCommercial2]![Form1]![Combo431] & "-" & (Format([Text48], "0000"))
    [DataEntryID].Value = [Forms]![frmCommercial2]![Combo23]
    End Sub



    Private Sub Length_AfterUpdate()
    Dim varYear As Integer
    Dim varAssessment As Integer
    Dim varCollector As Integer
    Dim varFishCount As Integer

    varYear = [Forms]![frmCommercial2]![Form1]![Text161]
    varAssessment = [Forms]![frmCommercial2]![Form1]![Text367]
    varCollector = [Forms]![frmCommercial2]![Form1]![CollectorID]
    varFishCount = DLookup("[CountofIndividualFishID]", "qry2012", "[CollectionYear]=" & varYear & " And [CollectorID]=" & varCollector & " And [AssessCodeID]=" & varAssessment)

    If (IsNull(varFishCount)) Then
    [Text51] = 0
    [Text53] = 1
    ElseIf Not (IsNull(varFishCount)) Then
    [Text51] = varFishCount
    [Text53] = varFishCount + 1
    End If
    [FishNum] = [Forms]![frmCommercial2]![Form1]![Text161] & "-" & [Forms]![frmCommercial2]![Form1]![Text367] & "-" & [Forms]![frmCommercial2]![Form1]![CollectorID] & "-" & (Format([Text53], "0000"))
    End Sub

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Well, you haven't said what the error was. An undeclared variable is a Variant by default. Variant can hold a Null and Integer can't, so I suspect the DLookup is returning a Null. The first code would handle that, the second wouldn't.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Fish218 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    68
    Got it. Improper use of Null was the error, so what you said makes sense.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    By the way, most of us use the first bit of the variable name to indicate what data type it is, so

    Dim intWhatever As Integer
    Dim varWhatever As Variant
    Dim dteWhatever As Date

    I assume you're using var to indicate it's a variable, which is fine. I'm just throwing it out there. The important thing about a naming convention is that it has meaning to you.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Fish218 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    68
    Good assumption on your part and a lack of Access naming convention knowledge on mine. Thanks for the tip.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Using DCount with Long integer
    By Dominaz in forum Access
    Replies: 5
    Last Post: 12-06-2011, 05:22 AM
  2. How to Set an Integer to Nothing
    By orcinus in forum Programming
    Replies: 4
    Last Post: 05-17-2011, 11:21 AM
  3. Integer not incrementing
    By jgelpi16 in forum Programming
    Replies: 2
    Last Post: 01-31-2011, 02:40 PM
  4. Adding column as INTEGER makes it a long integer?
    By luckycharms in forum Programming
    Replies: 2
    Last Post: 10-20-2010, 02:47 PM
  5. How to ADD integer (vba)
    By Computer202 in forum Programming
    Replies: 24
    Last Post: 03-13-2010, 08:50 AM

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