Results 1 to 5 of 5

Thread: Access 2007 VBA - Looking For A Better Way

  1. #1

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Access 2007 VBA - Looking For A Better Way

    I have an Access 2007 database application that I’m taking over. In one of the tables there are 13 fields, and I need to run a query to see if any of those fields contains a zero. The typical entry in these fields are numbers like 0.8, 1.4, 0.5, 9.1, 0.0 – I just need to know the COUNT of how many fields, for each of the around 2,500 records, contains a 0.0. I have:
    vb Code:
    1. Dim db As DAO.Database
    2. Set db = Access.Application.CurrentDb
    3. Dim rsCount As DAO.Recordset
    4. Dim sSQL As String
    5. Dim Counter As Integer
    6. Dim strPOCode As String
    7. Set rsCount = CurrentDb.OpenRecordset("SELECT * FROM table")
    8. Do While Not rsCount.EOF
    9.  
    10.     strPOCode = rsCount.Fields("FieldName")
    11.    
    12.     If rsCount.Fields("Field1").Value = 0 Then
    13.        Counter = Counter + 1
    14.     End If
    15.    
    16.     If rsCount.Fields("Field2").Value = 0 Then
    17.        Counter = Counter + 1
    18.     End If
    19.  
    20.     If rsCount.Fields("Field3").Value = 0 Then
    21.        Counter = Counter + 1
    22.     End If
    23.  
    24. 'etc etc on through all 13 fields
    25.  
    26.     sSQL = "UPDATE table "
    27.     sSQL = sSQL & "SET  CounterFieldInTable = " & Counter
    28.     sSQL = sSQL & " WHERE FieldName = '" & strPOCode & "' "
    29.     'Debug.Print sSQL
    30.     db.Execute sSQL
    31.     Counter = 0
    32.     rsCount.MoveNext
    33. Loop
    This works just fine....but I'm not real well versed in Access VBA (or any other kind of VBA for that matter) so I'm wondering if there is a better way. It loops through all 2,500 and change records pretty quickly....it just looks a little "sloppy".

    Thanks.
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  2. #2
    Fanatic Member dmaruca's Avatar
    Join Date
    May 2006
    Location
    Jacksonville, FL
    Posts
    577

    Re: Access 2007 VBA - Looking For A Better Way

    If you're looking for style suggestions, I'd say that everything looks fine. It's not sloppy at all!

    The only things I'd point out is that you should probably get into the habit of not assigning variables like strPOCode until just before you need it. The assignment was done 50 something lines before it was needed and the POCode is still valid in the recordset object... whoever inherited the code would have to search. You also created the db object and didn't use it.

    You did good being explicit with DAO. I'm not really a fan of hungarian notation (personal opinion) since it was created to take care of a problem in a different language than VBA, and VBA doesn't have that problem of excessive typecasting to the point of not knowing what type of variable you're looking at. I hope this was the kind of feedback you were looking for. Have a great day!

  3. #3
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Access 2007 VBA - Looking For A Better Way

    A single statement can do the job for you in one hit:
    Code:
    DoCmd.RunSQL "UPDATE Table1 SET ZeroCount = " & _
                 "-(Field1=0)-(Field2=0)-(Field3=0)-(Field4=0)-(Field5=0)-(Field6=0)-(Field7=0)" & _
                 "-(Field8=0)-(Field9=0)-(Field10=0)-(Field11=0)-(Field12=0)-(Field13=0)"
    You can also replace DoCmd.RunSQL with CurrentDb.Execute

    If Field1 = 0 then (Field1=0) = True = -1, otherwise (Field1=0) = False = 0
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  4. #4
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Access 2007 VBA - Looking For A Better Way

    Code:
        dim rst as dao.recordset
        dim strSql as string
        dim lngR as long, lngMR as long
        dim lngFld as long
        dim blnHasZero as boolean
    
        strsql = "SELECT fld1, fld2, fld3, fld4, HasZero from tblData"
    
        set rst = dbengine(0)(0).openrecordset(strsql)
        if rst.eof then
            msgbox "No records",vbokonly+vbinformation
        else
            rst.movelast
            rst.movefirst
            lngmr=rst.recordcount
    
    '---- loop through all records
            for lngr = 1 to lngmr
                 blnHasZero=false
    
    '---- change 3 for max field -1 (fields count from 0)
                 for lngFld = 0 to 3
                     if rst(lngfld)=0 then 
                          blnHasZero=true
                          exit for
                     endif
                 next
             
    
                 if blnHasZero then
                      rst.edit
                      rst("HasZero")=true
                      rst.update
                 end if
                 rst.movenext
    
    '---- option to add a percentage bar here if loads of records using lngr and lngmr
    
            next
        endif
        rst.close
        set rst = nothing

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  5. #5
    Fanatic Member dmaruca's Avatar
    Join Date
    May 2006
    Location
    Jacksonville, FL
    Posts
    577

    Re: Access 2007 VBA - Looking For A Better Way

    Ecniv, your code is less readable than the OP.

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