|
-
Feb 21st, 2012, 11:07 AM
#1
Thread Starter
Frenzied Member
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:
Dim db As DAO.Database
Set db = Access.Application.CurrentDb
Dim rsCount As DAO.Recordset
Dim sSQL As String
Dim Counter As Integer
Dim strPOCode As String
Set rsCount = CurrentDb.OpenRecordset("SELECT * FROM table")
Do While Not rsCount.EOF
strPOCode = rsCount.Fields("FieldName")
If rsCount.Fields("Field1").Value = 0 Then
Counter = Counter + 1
End If
If rsCount.Fields("Field2").Value = 0 Then
Counter = Counter + 1
End If
If rsCount.Fields("Field3").Value = 0 Then
Counter = Counter + 1
End If
'etc etc on through all 13 fields
sSQL = "UPDATE table "
sSQL = sSQL & "SET CounterFieldInTable = " & Counter
sSQL = sSQL & " WHERE FieldName = '" & strPOCode & "' "
'Debug.Print sSQL
db.Execute sSQL
Counter = 0
rsCount.MoveNext
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|