Page 1 of 3 123 LastLast
Results 1 to 15 of 35
  1. #1
    mikesal57 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    53

    Replacing Data

    Hi All ...




    First time here and hopefully for a while as I'm learning Access
    I have a large table stored. Maybe 200 columns wide and 250,000 + rows.
    I need at least , for now, 37 columns with 1's and 0's in it. I need to replace
    the 0's with a blank....I tried "REPLACE" option but the process halts and doesn't finish.
    I saw a video with making 2 queries for a REPLACE & UPDATE , but making 2 for each column
    is too much.....Any solutions?
    Thxs
    Mike

  2. #2
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,165
    That's a lot of columns. I wonder if your data is properly normalized?

    Anyway, maybe something like this?

    Code:
    Public Sub SetZerosToNull()
    On Error GoTo ErrHandler
        Dim db As DAO.Database
        Dim column_names() As String
        Dim column_name As Variant
        Dim update_qry As String
        
        Set db = CurrentDb
        
        'REPLACE THESE COLUMN NAMES WITH YOUR ACTUAL COLUMN NAMES
        column_names = Split("[column1],[column2],[column3],[column4]", ",")
        
        For Each column_name In column_names
            'REPLACE THE TABLE_NAME_HERE WITH YOUR TABLE NAME
            update_qry = "UPDATE [table_name_here] SET " & column_name & " = null WHERE " & column_name & " = 0;"
            
            'Debug.Print update_qry
            db.Execute update_qry, dbFailOnError
        Next
    
    ExitHandler:
        Set db = Nothing
        Exit Sub
    
    ErrHandler:
        MsgBox Err.Description, , "Error #" & Err.Number
        Resume ExitHandler
    End Sub

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  4. #4
    mikesal57 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    53
    Quote Originally Posted by kd2017 View Post
    That's a lot of columns. I wonder if your data is properly normalized?

    Anyway, maybe something like this?

    Code:
    Public Sub SetZerosToNull()
    On Error GoTo ErrHandler
        Dim db As DAO.Database
        Dim column_names() As String
        Dim column_name As Variant
        Dim update_qry As String
        
        Set db = CurrentDb
        
        'REPLACE THESE COLUMN NAMES WITH YOUR ACTUAL COLUMN NAMES
        column_names = Split("[column1],[column2],[column3],[column4]", ",")
        
        For Each column_name In column_names
            'REPLACE THE TABLE_NAME_HERE WITH YOUR TABLE NAME
            update_qry = "UPDATE [table_name_here] SET " & column_name & " = null WHERE " & column_name & " = 0;"
            
            'Debug.Print update_qry
            db.Execute update_qry, dbFailOnError
        Next
    
    ExitHandler:
        Set db = Nothing
        Exit Sub
    
    ErrHandler:
        MsgBox Err.Description, , "Error #" & Err.Number
        Resume ExitHandler
    End Sub
    thank you so much ...will give it a shot ....just got to find out where VBA's go in Access

  5. #5
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    With 200 fields in a table your data is almost certainly not normalized.
    Tables should be tall (many records) and thin ( Not 200 fields!)

    What is the datatype of the fields you are trying to Null? Some datatypes are a P.I.T.A.

    Have you tried looping through the table?

    Code:
        Dim db As DAO.Database
        Dim rs As DAO.Recordset
        Dim strSql As String
      
        strSql = "select * from  YoutTableName"  'change as appropriate
        
        Set db = CurrentDb()
        Set rs = db.OpenRecordset(strSql)
    
    
        If rs.BOF And rs.EOF Then
            GoTo MyExit
        End If
    
    
        Do Until rs.EOF
    
            rs.Edit
            rs!YourFieldName = Null  'change as appropriate
            rs!YourFieldName = Null  'change as appropriate
            rs!YourFieldName = Null  'change as appropriate
            rs.Update
    
            rs.MoveNext
        Loop
    
    
    MyExit:
        rs.Close
        Set rs = Nothing
        Set db = Nothing
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  6. #6
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,165
    Quote Originally Posted by mikesal57 View Post
    thank you so much ...will give it a shot ....just got to find out where VBA's go in Access
    You're welcome. Press Alt-F11 and in the toolbar click on insert module -OR- in the ribbon go to Create -> Macros & Code -> Module

  7. #7
    mikesal57 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    53
    having an issue running it....
    Attached Thumbnails Attached Thumbnails 1.JPG   2.JPG   3.JPG  

  8. #8
    mikesal57 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    53
    I get this now after I capitalized column headers...
    Attached Thumbnails Attached Thumbnails Capture.JPG  

  9. #9
    mikesal57 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    53

    more

    add this to list
    Attached Thumbnails Attached Thumbnails Capture.jpg  

  10. #10
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,165
    That last one is very weird. That tells me that references aren't there that should be by default. As you can see in my debug window the code should at least compile. Beyond that if there's an issue with the code it's in the SQL statement.

    Go to Tools -> References... and make sure at least these boxes are checked. If something's not there scroll down to find it, what's not checked will be in alphabetical order in the listbox (it's a long list).

    Click image for larger version. 

Name:	Untitled.png 
Views:	28 
Size:	76.1 KB 
ID:	44688

  11. #11
    mikesal57 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    53
    Quote Originally Posted by kd2017 View Post
    That last one is very weird. That tells me that references aren't there that should be by default. As you can see in my debug window the code should at least compile. Beyond that if there's an issue with the code it's in the SQL statement.

    Go to Tools -> References... and make sure at least these boxes are checked. If something's not there scroll down to find it, what's not checked will be in alphabetical order in the listbox (it's a long list).



    Click image for larger version. 

Name:	Untitled.png 
Views:	28 
Size:	76.1 KB 
ID:	44688

    got this...
    Attached Thumbnails Attached Thumbnails Capture.jpg  

  12. #12
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,165
    Add 'Microsoft Office 16.0 Access database engine Object Library' per my previous post.

  13. #13
    mikesal57 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    53
    Quote Originally Posted by kd2017 View Post
    Add 'Microsoft Office 16.0 Access database engine Object Library' per my previous post.
    will KD...let you know what happens..

  14. #14
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,165
    FYI this is all assuming that your fields are numeric datatypes that accept null values. For example:
    Click image for larger version. 

Name:	Untitled.png 
Views:	26 
Size:	13.9 KB 
ID:	44691

  15. #15
    mikesal57 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    53
    It worked somehow....but not from the macro I created...

    Pictures of what happen when I rAn Macro....and.......pictures of when I RAN it from ALT-11 screen...
    Attached Thumbnails Attached Thumbnails 1.JPG   2.JPG   3.jpg   4.JPG  

Page 1 of 3 123 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 01-09-2019, 02:41 PM
  2. Replacing one value for another
    By Richiebob in forum Programming
    Replies: 10
    Last Post: 05-06-2013, 11:33 AM
  3. Replacing data in a field.
    By darrellx in forum Queries
    Replies: 4
    Last Post: 08-21-2011, 10:33 AM
  4. Compiling Data, Not Replacing
    By helpaccess in forum Access
    Replies: 12
    Last Post: 03-08-2011, 03:13 PM
  5. Replacing data in Table
    By JackT in forum Import/Export Data
    Replies: 1
    Last Post: 08-23-2010, 10:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums