Solved

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

Posted on 2015-01-12
10
582 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 120

Expert Comment

by:Rey Obrero (Capricorn1)
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
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 50

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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

717 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