473,385 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,385 developers and data experts.

Dev Tools: Fill immediate window with Table columns

1,271 Expert 1GB
To celebrate my 800th Bytes post I'm posting my first article. I plan to post a number of articles in a "Dev Tools" series; those being articles containing tools for developers to use as opposed to simply transferring knowledge about how to do something. The first tool is the subroutine "GetColumnNames."

Intellisense is nice but not always available and limited to one field at a time. I often find myself not quite sure about the spelling of a particular field name - is it OrderQty or QtyOrdered, CustPO or CustPONO, stuff like that used to drive me mad. Finally, some time ago, I wrote a routine to list a table's column names in the immediate window. Over time, I expanded it to return the information in various formats, depending on the need.

As of now, this routine will spit out to the immediate window one of these formats:
1. the field or column name only
2. TableName.FieldName.FieldType
3. rs1!FieldName=X
4. X=rs1!FieldName
In formats 3 and 4, X is literal; I paste the returned code into the module I'm working on and replace each X with the appropriate field name or value. Saves a lot of typing when I have to build, copy or update records in VBA. Likewise, the "rs1" may or may not have to be changed to whatever rs object name I'm using in the given instance.

Finally, the Table and Field name parameters are optional and represent search strings to match.
GetColumnNames(1,"PREmployee","W2") will return all column names beginning with "W2" from table "PREmployee".
GetColumnNames(2,"PR") will return every Table Name, the Field Name and the Field Type from every table beginning with "PR"
GetColumnNames(3,"PREmployee","YTD") might return something like this:
rs1!YTDWages=X
rs1!YTDFedWH=X
rs1!YTDNet=X ... and so on for all columns beginning YTD

I have used Enmerations so that the user doesn't have to remember that 1=gcnSimple,2=gcnComplex,3=gcnCopyTo and 4=gcnCopyFrom.

Enough babbling, on with the code.
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3. Public Enum gcnTypes
  4.     gcnSimple = 1
  5.     gcnComplex = 2
  6.     gcnCopyTo = 3
  7.     gcncopyfrom = 4
  8.  
  9. End Enum
  10.  
  11. '---------------------------------------------------------------------------------------
  12. ' Procedure : GetColumnNames
  13. ' Author    : Jim
  14. ' Date      : 4/28/2014
  15. ' Purpose   : Returns Table Field names in optional formats to the immediate window
  16. '             Optional paramerters allow selective return of all tables and fields or wildcard matches
  17. '             Return formats are
  18. '               Field Name only (gcnSimple)
  19. '               TableName.FieldName.Type (gcnComplex)
  20. '               rs1!FieldName=x  (gcnCopyTo)
  21. '               x=rs1!FieldName  (gcnCopyFrom)
  22. '---------------------------------------------------------------------------------------
  23. '
  24. Public Sub GetColumnNames(ReplyType As gcnTypes, Optional TableName_str As String, Optional FieldPrefix_str As String)
  25. ' reply types are 1=simple (field names only)
  26. '                 2=complex (table name, field name, field type)
  27. '                 3=Move to
  28. '                 4=Move from
  29. '
  30. ' returns data in the immediate window
  31.     Dim tbl As DAO.TableDef
  32.     Dim fld As DAO.Field
  33.     Dim fldTypes(23) As String
  34.     Dim fldTyp As Integer
  35.     Dim fldDesc As String
  36.  
  37.    On Error GoTo GetColumnNames_Error
  38.  
  39.     fldTypes(1) = "Boolean"
  40.     fldTypes(2) = "Byte"
  41.     fldTypes(3) = "Integer"
  42.     fldTypes(4) = "Long"
  43.     fldTypes(5) = "Currency"
  44.     fldTypes(6) = "Single"
  45.     fldTypes(7) = "Double"
  46.     fldTypes(8) = "Date"
  47.     fldTypes(9) = "Binary"
  48.     fldTypes(10) = "Text"
  49.     fldTypes(11) = "Long Binary"
  50.     fldTypes(12) = "Memo"
  51.     fldTypes(13) = "Attachment" '101
  52.     fldTypes(14) = "Complex Byte" '102
  53.     fldTypes(15) = "Complex Integer"
  54.     fldTypes(16) = "Complex Long"
  55.     fldTypes(17) = "Complex Single"
  56.     fldTypes(18) = "Complex Double"
  57.     fldTypes(19) = "Complex GUID"
  58.     fldTypes(20) = "Complex Decimal"
  59.     fldTypes(21) = "Complex Text"  ' 109
  60.     fldTypes(22) = "Other"
  61.  
  62.     ' Print the header.
  63.    On Error GoTo GetColumnNames_Error
  64.  
  65.     ' Loop through all the table definitions.
  66.     For Each tbl In CurrentDb.TableDefs
  67.         If Len(Nz(TableName_str)) = 0 Or (Left(tbl.Name, Len(TableName_str)) = TableName_str) Then
  68.             For Each fld In tbl.Fields
  69.                 fldTyp = fld.Type
  70.                 If fldTyp > 100 And fldTyp <= 109 Then
  71.                     fldTyp = fldTyp - 88 ' 101 becomes 13
  72.                 End If
  73.                 If fldTyp > 0 And fldTyp <= 22 Then
  74.                     fldDesc = fldTypes(fldTyp)
  75.                 Else
  76.                     fldDesc = fld.Type & " Other"
  77.                 End If
  78.  
  79.                         'only include fields matching the name requested
  80.                 If Len(Nz(FieldPrefix_str)) = 0 Or (Left(fld.Name, Len(FieldPrefix_str)) = FieldPrefix_str) Then
  81.                      Select Case ReplyType
  82.                         Case gcnSimple
  83.                                     Debug.Print fld.Name
  84.                         Case gcnComplex
  85.                                  Debug.Print tbl.Name & "." & fld.Name & "." & fldDesc
  86.                         Case gcnCopyTo
  87.                                  Debug.Print "rs1!" & fld.Name & "= x"
  88.                         Case gcncopyfrom
  89.                                 Debug.Print "x=rs1!" & fld.Name
  90.                      End Select
  91.                 End If
  92.                 Next fld
  93.             End If
  94.  
  95.     Next tbl
  96.  
  97.    On Error GoTo 0
  98.    Exit Sub
  99.  
  100. GetColumnNames_Error:
  101.  
  102.     MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure GetColumnNames of Module PublicCode_vb"
  103.     Resume Next
  104.  
  105.    On Error GoTo 0
  106.    Exit Sub
  107.  
  108. End Sub
  109.  
Paste the code into a public module and just call it from the immediate window when the need arises. Feel free to change the name to GCN or some other short name.

Jim
Apr 29 '14 #1
6 5605
Seth Schrock
2,965 Expert 2GB
I can't wait to try this. Normally when I'm developing databases, I'm running a system with dual monitors plus I will have printed out (on paper) a copy of the relationships window so that I have the tables names, but I don't always have these features available to me. Plus the relationships window doesn't give data types. Does it just give the generic data type (like Number) or is it specific (integer, long integer, decimal, etc.)?
Apr 29 '14 #2
jimatqsi
1,271 Expert 1GB
Seth,
You can see the complete list of data types described in the array fldTypes. I'm wondering if I need to look at expanding the list for later versions of Access.

Jim
Apr 29 '14 #3
Seth Schrock
2,965 Expert 2GB
I added a bit of functionality to your code to make it a little easier to read so I thought that I would pass it on.

First I added another "view" to your code that just displayed the field name and the data type. I figure since I'm having to tell your sub which table I'm looking for, there isn't much point in having it display it again for each field. Also, with just the period separating the different columns it was a little hard to read in the immediate window. So I added a private function that would add spaces to the end of the field name so that the data types would then line up.
Expand|Select|Wrap|Line Numbers
  1. Private Function AddPadding(Text As String, PadTo As Integer)
  2.  
  3. Text = Text & Space(PadTo - Len(Text))
  4.  
  5. AddPadding = Text
  6.  
  7. End Function
I then use it like the following
Expand|Select|Wrap|Line Numbers
  1. Debug.Print AddPadding(fld.Name, 50) & AddPadding(fldDesc, 20)
  2.  
  3. Produces:
  4.  AcctTypeID_pk                                    Long                
  5.  AcctType                                         Text                
  6.  TicketTypeID_fk                                  Long     
I'm also a little confused as to what gcnCopyTo and gcnCopyFrom are for. What did you have in mind when you created those?
Apr 29 '14 #4
jimatqsi
1,271 Expert 1GB
Eliminating the table name when there's only one table involved seems like a good idea. What I was trying to say in paragraph 3 is that the gcnCopyto/From are for automated code generation rather than reference/documentation. No matter how many fields are in a table I can get all the basic code generated for a few very common situations. So I don't have to type all that rs1!fieldname= again and again, this just prepares that code for me.

I would make a slight modification to your common length fld.Name display. I would use (longest fld.Name +1) rather than a fixed 50 length.

Thanks,

Jim
Apr 29 '14 #5
Seth Schrock
2,965 Expert 2GB
I had thought of that, but that would mean looping through the table fields once to find the longest one and then loop through again to do the Debug.Print and I don't think that I have ever had a field name over 30 characters, so I choose to use the fixed length.

I hadn't paid attention to the fact that the table name was optional. To account for this, I have now added the following code at the top of the table loop, inside the If-Then statement.
Expand|Select|Wrap|Line Numbers
  1. Debug.Print "---------------------------------------"
  2.             Debug.Print "Table Name: " & tbl.Name
  3.             Debug.Print "---------------------------------------"
This way the table name is only printed once.

I really like your tool. I can't wait to get another project started where I can use it.
Apr 29 '14 #6
jimatqsi
1,271 Expert 1GB
One little change that should be made because many of us deal with designs that are less than perfect. In the Debug statements for gcnCopyTo and gcnCopyFrom, change references to fld.Name to
Expand|Select|Wrap|Line Numbers
  1. "[" & fld.Name & "]" 
Because you'll run into some field names with spaces in them.

Jim
May 1 '14 #7

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

Similar topics

4
by: Tony | last post by:
Hello, Using Access2000, WinNT. Using the Immediate Window, I can view the results of my VBA code as it performs operations/calculations on my data. Now that I have all the bugs worked out,...
2
by: Jon Davis | last post by:
How do I get the Immediate window back? It seems to be gone. Command window doesn't work. "View" menu items don't seem to show it Jon
7
by: rodchar | last post by:
Hey all, when i'm in debug mode, is there a way to get to a certain record in a dataset (say like in the Immediate window or something)? thanks, rodchar
11
by: sara | last post by:
I am trying my first functions in my code. I have a set of queries that runs to create temp tables with the right data for some reports. They can run for a long time, so I want the user to know...
7
by: MLH | last post by:
I use this to list table fieldnames to the debug window. Can it be modified to perform similar objective for saved query objects? Would it be best to trash this completely and write a procedure...
1
by: Al_C | last post by:
Hi, I must have done something to my VBExpress configuration as I no longer can see anything in my immediate window when I try to output to it using debug.write("something") Any suggestions....
5
by: vul | last post by:
In VB6 I used to use Immediate Window to get or change values of variables. It is very convenient while debugging. I used drag and drop operation to paste the variable name into Immediate Window....
2
by: dgk | last post by:
Using VS2005 Standard Edtion, I have an Immediate window during ASP debugging. My co-worker, using VS2005 Team Edtion does not have an Immediate window. He does have a Command window, which is sort...
6
by: Frank Rizzo | last post by:
I am using the Immediate Window a lot to see the progress of the application. In VS2003, if your cursor was at the very bottom, the window would scroll down whenever something new showed up. If...
5
by: Romulo NF | last post by:
Greetings, I´m back here to show the new version of the drag & drop table columns (original script ). I´ve found some issues with the old script, specially when trying to use 2 tables with...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.