• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 798
  • Last Modified:

Set all values in table to NULL with VBA In Access 2010

I have a small table with about 73 records.  I am migrating data from another data source.  I want to keep the auto-numbers as these are the same in both tables.  what would be the easiest was to null out all column values (there are a lot of columns).  None are requires except the ID column, which is the auto-number.  I know it would be a loop, but how do I loop through each field in each column of the table other than the IDDeal column?

Sandra
0
ssmith94015
Asked:
ssmith94015
  • 4
  • 2
  • 2
  • +2
1 Solution
 
SimonCommented:
If you're updating the table based on the  IDDEAL auto-number, you can set any columns that you're not providing new data for to null in the update query.

I'm assuming you want to join your destination table to the incoming table on the ID and update every other field in the destination table?
0
 
Rey Obrero (Capricorn1)Commented:
create an update query, using the QBE
place all the fields to the grid except for the ID column
in the Update to: row, place null or ""
0
 
ssmith94015Author Commented:
I realized I could user an Update query, BUT (and I did not bulid this database, I am just stuck with it) many of the tables have 250 columns and I am hoping to use a procedure that will loop through the fields so all I have to do is pass the name of the table and be sure it does not update the auto-number ID column.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ssmith94015Author Commented:
Actuall, solved my myself, below is the code.

Public Sub DatagMitragation()
    Call SetToNull("tblDeal")
End Sub

Public Sub SetToNull(strTableName As String)
'Sets all fields in the table to null other than the DealID columns
'this gets it ready for data migration, but be sure the auto-numbers are the
'as in the migrated table.
Dim rst As DAO.Recordset
Dim fld As DAO.Field
Dim i As Integer

Set rst = CurrentDb.OpenRecordset(strTableName)

With rst
    rst.MoveFirst
        Do While Not .EOF
            For i = 0 To .Fields.Count - 1
             Set fld = .Fields(i)
             Debug.Print fld.Name
                If fld.Name <> "DealID" Then
                    .Edit
                    .Fields(i) = Null
                    .Update
                End If
            Next i
        rst.MoveNext
        Loop
End With
     Set fld = Nothing
     rst.Close
     Set rst = Nothing
     
     
End Sub
0
 
SimonCommented:
Perhaps it would help to explain a bit more about what you're trying to achieve... Doess the new data you're importing have matching ID numbers and presumably many identically named fields to the tables you want to blank out?

If the new data has ALL the same fields as the tables you're updating, you can just build and run the update queries without worrying about setting all fields to NULL first.

Or, you might consider renaming the OLD tables and renaming the incoming tables to directly replace them?

How many tables are you doing this process on?
0
 
Rey Obrero (Capricorn1)Commented:
try this codes, place it in a regular module

Function fncSelectAllFieldsExcept(TableName As String, Optional ExceptField As String) As String
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim strFieldName As String
Dim strFieldList As String
Dim intField As Integer
Set db = CurrentDb
Set tdf = db.TableDefs(TableName)

With tdf
    For intField = 0 To .Fields.Count - 1
    strFieldName = .Fields(intField).Name
    If strFieldName <> ExceptField Then
    strFieldList = strFieldList & ", [" & strFieldName & "]= Null"
    End If
    Next intField
End With
If Len(strFieldList) > 0 Then
    fncSelectAllFieldsExcept = Mid(strFieldList, 3)
End If
End Function

Open in new window


Sub CreateUpdateSQL(sTable As String, sField As String)
Dim sSql As String
sSql = "Update " & sTable & " Set " _
    & fncSelectAllFieldsExcept(sTable, sField)
Debug.Print sSql
CurrentDb.Execute sSql, dbFailOnError
End Sub

Open in new window


then call the sub like this

CreateUpdateSQL "NameofTable","NameOfAutoNumberField"
0
 
Nick67Commented:
You beat me to it, Sandra.
That's exactly how I'd get it done, at first flush.
@Rey's posted code that would build a string to do the heavy lifting with SQL instead of the recordset code you and I would use.
His is different in that he's only walking the fields collection and only executing a single massive SQL command

But yours is essentially a loop within a loop.
You're walking down a recordset and the fields collection
That works but is a bit wasteful.

Upon consideration, I'd walk the fields collection, but then issue a single SQL command for each field.

Dim rst As DAO.Recordset
Dim fld As DAO.Field
Dim i As Integer

 Set rst = CurrentDb.OpenRecordset(strTableName)
 With rst
             For i = 0 To .Fields.Count - 1
              Set fld = .Fields(i)
              Debug.Print fld.Name
                 If fld.Name <> "DealID" Then
                     CurrentDb.Execute "Update " & strTableName & " Set " & fld.Name & " =  Null",  dbFailOnError
                 End If
             Next i
         Loop
 End With
 Set fld = Nothing
 rst.Close
 Set rst = Nothing

Open in new window


A little easier to debug than a huge monolithic statement but a little more efficient than a double loop.
0
 
ssmith94015Author Commented:
I do like Rey's also, a bit more elegant.
0
 
Gustav BrockCIOCommented:
> Solved myself.

Well, it will work but ...
Here is how - updating row by row, not field by field(!) and a simpler loop:

With rst
    .MoveFirst
    Do While Not .EOF
        .Edit
        For Each fld In .Fields
            Debug.Print fld.Name
            If fld.Name <> "DealID" Then
                fld.Value = Null
            End If
        Next
        .Update
        .MoveNext
    Loop
    .Close
End With

/gustav
0
 
ssmith94015Author Commented:
Solved myself.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now