Solved

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

Posted on 2015-01-12
10
385 Views
Last Modified: 2015-01-17
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
Comment
Question by:ssmith94015
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 18

Expert Comment

by:Simon
ID: 40545525
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
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 40545528
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
 

Author Comment

by:ssmith94015
ID: 40545543
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
 

Accepted Solution

by:
ssmith94015 earned 0 total points
ID: 40545609
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
 
LVL 18

Expert Comment

by:Simon
ID: 40545611
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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 119

Expert Comment

by:Rey Obrero
ID: 40545614
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
 
LVL 26

Expert Comment

by:Nick67
ID: 40545674
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
 

Author Comment

by:ssmith94015
ID: 40545696
I do like Rey's also, a bit more elegant.
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40546070
> 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
 

Author Closing Comment

by:ssmith94015
ID: 40554950
Solved myself.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now