Solved

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

Posted on 2015-01-12
10
350 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:SimonAdept
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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:SimonAdept
Comment Utility
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
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
Comment Utility
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
Comment Utility
I do like Rey's also, a bit more elegant.
0
 
LVL 49

Expert Comment

by:Gustav Brock
Comment Utility
> 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
Comment Utility
Solved myself.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

772 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

10 Experts available now in Live!

Get 1:1 Help Now