473,379 Members | 1,245 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Need Access VBA code to drop empty fields in a table

126 100+
Hello everyone, I have imported an excel file into an Access table, but the table contains some empty fields, does anyone know how to delete those empty fields using Access VBA?

Thanks in Advance.
Jul 14 '08 #1

✓ answered by ADezii

'Completely' replace the previous code with this code block, it should do the trick. Plug your own Table name into the Constant conTABLE_NAME. Keep in touch and let me know how you make out.

P.S. - You cannot run this code from a Form whose Record Source is based on a Table/Query which consists of the Field(s) to be deleted!
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As Database
  2. Dim MyRS As DAO.Recordset
  3. Dim intFieldCounter As Integer
  4. Dim intNumOfFields As Integer
  5. Dim intNumOfRecords As Integer
  6. Dim intNumOfEmptyFields As Integer
  7. Dim astrFieldsToDelete() As String
  8. Dim intCounter As Integer
  9. Dim tdf As DAO.TableDef
  10. Const conTABLE_NAME As String = "Employees"     'Replace
  11.  
  12. Set MyDB = CurrentDb
  13. Set MyRS = MyDB.OpenRecordset(conTABLE_NAME, dbOpenDynaset)
  14.  
  15. MyRS.MoveLast: MyRS.MoveFirst
  16.  
  17. intNumOfFields = MyRS.Fields.Count
  18.  
  19. 'Redimension Array to the Maximum Number of Fields to Delete,
  20. 'namely the Total Number of Fields
  21. ReDim astrFieldsToDelete(1 To intNumOfFields)
  22.  
  23. intNumOfRecords = MyRS.RecordCount
  24.  
  25. For intFieldCounter = 0 To intNumOfFields - 1
  26.   Do While Not MyRS.EOF
  27.    'Debug.Print MyRS.Fields(intFieldCounter).Name & " ==> " & _
  28.                'MyRS.Fields(intFieldCounter).Value
  29.     If IsNull(MyRS.Fields(intFieldCounter).Value) Then
  30.       intNumOfEmptyFields = intNumOfEmptyFields + 1
  31.     End If
  32.     MyRS.MoveNext
  33.   Loop
  34.     If intNumOfEmptyFields = intNumOfRecords Then   'ALL' Fields Empty
  35.       'Debug.Print "Must DELETE: " & MyRS.Fields(intFieldCounter).Name
  36.       astrFieldsToDelete(intFieldCounter) = MyRS.Fields(intFieldCounter).Name
  37.     End If
  38.     intNumOfEmptyFields = 0     'Reset
  39.     MyRS.MoveFirst
  40. Next
  41.  
  42. MyRS.Close
  43. Set MyRS = Nothing
  44.  
  45. Set tdf = MyDB.TableDefs(conTABLE_NAME)
  46.  
  47. 'Delete valid Fields whose names are in the Array astrFieldsToDelete()
  48. For intCounter = LBound(astrFieldsToDelete) To UBound(astrFieldsToDelete)
  49.   If Len(astrFieldsToDelete(intCounter)) > 0 Then
  50.     'Debug.Print "In Array: " & astrFieldsToDelete(intCounter)
  51.     tdf.Fields.Delete astrFieldsToDelete(intCounter)
  52.   End If
  53. Next

15 9865
ADezii
8,834 Expert 8TB
This one was a little challenging, but here it goes. The following code will loop through all Fields in a Recordset, in this case one based on an Employees Table, and will list to the Immediate Window 'ALL' Fields which have 'NO' values in 'ALL' the Records in the Recordset. To subsequently DELETE those Fields would be even trickier yet since you cannot use the Delete Method on a member of the Fields Collection, and you cannot Delete a Field using SQL, DAO, ADO, etc. if it is contained in an active Recordset. Should you really need this functionality, I'm sure that I can accommodate you, just let me know.
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As Database
  2. Dim MyRS As DAO.Recordset
  3. Dim intFieldCounter As Integer
  4. Dim intNumOfFields As Integer
  5. Dim intNumOfRecords As Integer
  6. Dim intNumOfEmptyFields As Integer
  7.  
  8. Set MyDB = CurrentDb
  9. Set MyRS = MyDB.OpenRecordset("Employees", dbOpenDynaset)
  10.  
  11. MyRS.MoveLast: MyRS.MoveFirst
  12.  
  13. intNumOfFields = MyRS.Fields.Count
  14. intNumOfRecords = MyRS.RecordCount
  15.  
  16. For intFieldCounter = 0 To intNumOfFields - 1
  17.   Do While Not MyRS.EOF
  18.     If IsNull(MyRS.Fields(intFieldCounter).Value) Then
  19.       intNumOfEmptyFields = intNumOfEmptyFields + 1
  20.     End If
  21.     MyRS.MoveNext
  22.   Loop
  23.     If intNumOfEmptyFields = intNumOfRecords Then   'ALL' Fields Empty
  24.       Debug.Print MyRS.Fields(intFieldCounter).Name
  25.     End If
  26.     intNumOfEmptyFields = 0     'Reset
  27.     MyRS.MoveFirst
  28. Next
  29.  
  30. MyRS.Close
  31. Set MyRS = Nothing
Jul 14 '08 #2
JFKJr
126 100+
This one was a little challenging, but here it goes. The following code will loop through all Fields in a Recordset, in this case one based on an Employees Table, and will list to the Immediate Window 'ALL' Fields which have 'NO' values in 'ALL' the Records in the Recordset. To subsequently DELETE those Fields would be even trickier yet since you cannot use the Delete Method on a member of the Fields Collection, and you cannot Delete a Field using SQL, DAO, ADO, etc. if it is contained in an active Recordset. Should you really need this functionality, I'm sure that I can accommodate you, just let me know.
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As Database
  2. Dim MyRS As DAO.Recordset
  3. Dim intFieldCounter As Integer
  4. Dim intNumOfFields As Integer
  5. Dim intNumOfRecords As Integer
  6. Dim intNumOfEmptyFields As Integer
  7.  
  8. Set MyDB = CurrentDb
  9. Set MyRS = MyDB.OpenRecordset("Employees", dbOpenDynaset)
  10.  
  11. MyRS.MoveLast: MyRS.MoveFirst
  12.  
  13. intNumOfFields = MyRS.Fields.Count
  14. intNumOfRecords = MyRS.RecordCount
  15.  
  16. For intFieldCounter = 0 To intNumOfFields - 1
  17.   Do While Not MyRS.EOF
  18.     If IsNull(MyRS.Fields(intFieldCounter).Value) Then
  19.       intNumOfEmptyFields = intNumOfEmptyFields + 1
  20.     End If
  21.     MyRS.MoveNext
  22.   Loop
  23.     If intNumOfEmptyFields = intNumOfRecords Then   'ALL' Fields Empty
  24.       Debug.Print MyRS.Fields(intFieldCounter).Name
  25.     End If
  26.     intNumOfEmptyFields = 0     'Reset
  27.     MyRS.MoveFirst
  28. Next
  29.  
  30. MyRS.Close
  31. Set MyRS = Nothing

Thanks a lot for the reply, the above code successfully displaying the empty fields in my table (ImportedData). Now I would like to delete these empty fields from the table. It would really make my day if I can get this code.

I am tired of searching this code on internet.

Thanks.
Jul 14 '08 #3
ADezii
8,834 Expert 8TB
Thanks a lot for the reply, the above code successfully displaying the empty fields in my table (ImportedData). Now I would like to delete these empty fields from the table. It would really make my day if I can get this code.

I am tired of searching this code on internet.

Thanks.
The Names of the Fields to Delete would have to be stored in an Array, the Recordset would have to be closed, the Names would then have to be retrieved from the Array, and the Fields themselves deleted via DAO since we are already using it. There lies the logic, when I get a chance I will make an 'attempt' to implement it. BTW, don't beat yourself on this one, since this is rather specialized and not-mush-asked-for code. I don't think that you will find many references to this on the Internet. Check back this evening or at the latest tomorrow afternoon.
Jul 14 '08 #4
JFKJr
126 100+
The Names of the Fields to Delete would have to be stored in an Array, the Recordset would have to be closed, the Names would then have to be retrieved from the Array, and the Fields themselves deleted via DAO since we are already using it. There lies the logic, when I get a chance I will make an 'attempt' to implement it. BTW, don't beat yourself on this one, since this is rather specialized and not-mush-asked-for code. I don't think that you will find many references to this on the Internet. Check back this evening or at the latest tomorrow afternoon.
Thanks for the response, I understand your logic, but it seems to be difficult to implement as I am a new beginner to Access VBA. It would be of great help if I can get help on this from you.

I will definitely keep in touch with the post. Thanks, Have a great day!
Jul 14 '08 #5
ADezii
8,834 Expert 8TB
'Completely' replace the previous code with this code block, it should do the trick. Plug your own Table name into the Constant conTABLE_NAME. Keep in touch and let me know how you make out.

P.S. - You cannot run this code from a Form whose Record Source is based on a Table/Query which consists of the Field(s) to be deleted!
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As Database
  2. Dim MyRS As DAO.Recordset
  3. Dim intFieldCounter As Integer
  4. Dim intNumOfFields As Integer
  5. Dim intNumOfRecords As Integer
  6. Dim intNumOfEmptyFields As Integer
  7. Dim astrFieldsToDelete() As String
  8. Dim intCounter As Integer
  9. Dim tdf As DAO.TableDef
  10. Const conTABLE_NAME As String = "Employees"     'Replace
  11.  
  12. Set MyDB = CurrentDb
  13. Set MyRS = MyDB.OpenRecordset(conTABLE_NAME, dbOpenDynaset)
  14.  
  15. MyRS.MoveLast: MyRS.MoveFirst
  16.  
  17. intNumOfFields = MyRS.Fields.Count
  18.  
  19. 'Redimension Array to the Maximum Number of Fields to Delete,
  20. 'namely the Total Number of Fields
  21. ReDim astrFieldsToDelete(1 To intNumOfFields)
  22.  
  23. intNumOfRecords = MyRS.RecordCount
  24.  
  25. For intFieldCounter = 0 To intNumOfFields - 1
  26.   Do While Not MyRS.EOF
  27.    'Debug.Print MyRS.Fields(intFieldCounter).Name & " ==> " & _
  28.                'MyRS.Fields(intFieldCounter).Value
  29.     If IsNull(MyRS.Fields(intFieldCounter).Value) Then
  30.       intNumOfEmptyFields = intNumOfEmptyFields + 1
  31.     End If
  32.     MyRS.MoveNext
  33.   Loop
  34.     If intNumOfEmptyFields = intNumOfRecords Then   'ALL' Fields Empty
  35.       'Debug.Print "Must DELETE: " & MyRS.Fields(intFieldCounter).Name
  36.       astrFieldsToDelete(intFieldCounter) = MyRS.Fields(intFieldCounter).Name
  37.     End If
  38.     intNumOfEmptyFields = 0     'Reset
  39.     MyRS.MoveFirst
  40. Next
  41.  
  42. MyRS.Close
  43. Set MyRS = Nothing
  44.  
  45. Set tdf = MyDB.TableDefs(conTABLE_NAME)
  46.  
  47. 'Delete valid Fields whose names are in the Array astrFieldsToDelete()
  48. For intCounter = LBound(astrFieldsToDelete) To UBound(astrFieldsToDelete)
  49.   If Len(astrFieldsToDelete(intCounter)) > 0 Then
  50.     'Debug.Print "In Array: " & astrFieldsToDelete(intCounter)
  51.     tdf.Fields.Delete astrFieldsToDelete(intCounter)
  52.   End If
  53. Next
Jul 14 '08 #6
JFKJr
126 100+
'Completely' replace the previous code with this code block, it should do the trick. Plug your own Table name into the Constant conTABLE_NAME. Keep in touch and let me know how you make out.

P.S. - You cannot run this code from a Form whose Record Source is based on a Table/Query which consists of the Field(s) to be deleted!
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As Database
  2. Dim MyRS As DAO.Recordset
  3. Dim intFieldCounter As Integer
  4. Dim intNumOfFields As Integer
  5. Dim intNumOfRecords As Integer
  6. Dim intNumOfEmptyFields As Integer
  7. Dim astrFieldsToDelete() As String
  8. Dim intCounter As Integer
  9. Dim tdf As DAO.TableDef
  10. Const conTABLE_NAME As String = "Employees"     'Replace
  11.  
  12. Set MyDB = CurrentDb
  13. Set MyRS = MyDB.OpenRecordset(conTABLE_NAME, dbOpenDynaset)
  14.  
  15. MyRS.MoveLast: MyRS.MoveFirst
  16.  
  17. intNumOfFields = MyRS.Fields.Count
  18.  
  19. 'Redimension Array to the Maximum Number of Fields to Delete,
  20. 'namely the Total Number of Fields
  21. ReDim astrFieldsToDelete(1 To intNumOfFields)
  22.  
  23. intNumOfRecords = MyRS.RecordCount
  24.  
  25. For intFieldCounter = 0 To intNumOfFields - 1
  26.   Do While Not MyRS.EOF
  27.    'Debug.Print MyRS.Fields(intFieldCounter).Name & " ==> " & _
  28.                'MyRS.Fields(intFieldCounter).Value
  29.     If IsNull(MyRS.Fields(intFieldCounter).Value) Then
  30.       intNumOfEmptyFields = intNumOfEmptyFields + 1
  31.     End If
  32.     MyRS.MoveNext
  33.   Loop
  34.     If intNumOfEmptyFields = intNumOfRecords Then   'ALL' Fields Empty
  35.       'Debug.Print "Must DELETE: " & MyRS.Fields(intFieldCounter).Name
  36.       astrFieldsToDelete(intFieldCounter) = MyRS.Fields(intFieldCounter).Name
  37.     End If
  38.     intNumOfEmptyFields = 0     'Reset
  39.     MyRS.MoveFirst
  40. Next
  41.  
  42. MyRS.Close
  43. Set MyRS = Nothing
  44.  
  45. Set tdf = MyDB.TableDefs(conTABLE_NAME)
  46.  
  47. 'Delete valid Fields whose names are in the Array astrFieldsToDelete()
  48. For intCounter = LBound(astrFieldsToDelete) To UBound(astrFieldsToDelete)
  49.   If Len(astrFieldsToDelete(intCounter)) > 0 Then
  50.     'Debug.Print "In Array: " & astrFieldsToDelete(intCounter)
  51.     tdf.Fields.Delete astrFieldsToDelete(intCounter)
  52.   End If
  53. Next
Hello ADezii, The above code is working perfectly fine. Thanks a lot for your help. Its been a week that I have been trying on the code to make it work. At last I finally got the code :), I don't find any words to express how much I appreciate your help. Thank you very much :)
Jul 15 '08 #7
ADezii
8,834 Expert 8TB
Hello ADezii, The above code is working perfectly fine. Thanks a lot for your help. Its been a week that I have been trying on the code to make it work. At last I finally got the code :), I don't find any words to express how much I appreciate your help. Thank you very much :)
You are quite welcome, that's why we are all here.
Jul 15 '08 #8
ADezii, i just want to say how much i appreciate your work here. I've been trying to solve that problem for literally the whole of last week - and it's such a relief to have found the code to do it for me!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

thanks man i appreciate it so much :)
:)
:)
:)

hooorayyyyyyyyyyyyyy!
May 27 '09 #9
ADezii
8,834 Expert 8TB
@johnsmith000
That's not a problem, glad we were able to resolve the problem.
May 27 '09 #10
Dear ADezii,

I've been searching the internet to find a way to delete empty fields out of my databases and ran across your post and your code. It is simply beautiful and worked like a charm. YOU ARE MY HERO. Thank you so much for posting this code. I know it must have taken you some time to do it. I just want you to know how much appreciated you are -- I joined bytes.com just to be able to write you a THANK YOU. For all your time, just know how much you have helped others well beyond the individual who asked the original question. YOU ARE THE BEST!!
Sep 20 '09 #11
NeoPa
32,556 Expert Mod 16PB
ADezii,

I've told you about this before. Stop creating new aliases to congratulate yourself with!!

(Sorry - couldn't resist it :D)

To the new members posting their thanks - Welcome to Bytes! It's nice to see people registering just to say thank you. I hope you will both continue to benefit from our site :)

PS. We think ADezii's a bit of a star too ;)
Sep 20 '09 #12
ADezii
8,834 Expert 8TB
@chcorsa
Thanks for taking the time and going out of your way to write this Post. We are all here for the same reason at Bytes, and I am grateful just to be a Member. Take care.
Sep 20 '09 #13
ADezii
8,834 Expert 8TB
@NeoPa
It's Posts like this that make it all worthwhile, hey NeoPa?
Sep 20 '09 #14
NeoPa
32,556 Expert Mod 16PB
It's certainly nice when people sign up just to thank you ADezii. Two in one thread is a bonus. It's probably the power of good example :)
Sep 20 '09 #15
You saved me a lot of work! Thanks very much!
Nov 6 '14 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: yvan | last post by:
Approximately once a month, a client of ours sends us a bunch of comma-delimited text files which I have to clean up and then import into their MS SQL database. All last week, I was using a Cold...
2
by: qsweetbee | last post by:
I have a form(fAddUsers) in my database. It is continue form for data entry. Some fields are required fields. Some are optional fields. There is 1 particular filed(TokenExpirationDate)on the form...
6
by: B Blue | last post by:
here's the situation: a produce dept at a grocery store needs to generate a weekly sheet for ordering. This is how it looks: 5 columns across with the following headings: Product Description,...
5
by: Krechting | last post by:
Hi ALl, I have a code that checks if the documents in a hyperlink field are still where they should be. I use fileexist(). First I want to filter out all the hyperlink fields that are empty. I...
3
by: google | last post by:
I have a database with four table. In one of the tables, I use about five lookup fields to get populate their dropdown list. I have read that lookup fields are really bad and may cause problems...
11
by: my-wings | last post by:
I think I've painted myself into a corner, and I'm hoping someone can help me out. I have a table of books (tblBooks), which includes a field (strPubName) for Publisher Name and another field...
3
by: troy.forster | last post by:
I am sure this must be about the simplest thing to do. I can whip something off in 10 different languages and environments with no problem. But VisualStudio 2003 and ASP.NET is sooooo...
21
by: lesperancer | last post by:
I've got an access97 reporting mdb that pulls data (77,000 rows) from a sql server table into a local table to run reports if the local table is part of the reporting MDB, the insert statement...
3
by: ibeehbk | last post by:
Hi. I have a form made in xhtml. I test via vbscript to make sure none of the fields are empty and properly formatted (ie email). All the regular fields work. However, I have two drop down menus...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.