473,545 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dev Tools: Fill immediate window with Table columns

1,273 Recognized Expert Top Contributor
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.Field Name.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=g cnComplex,3=gcn CopyTo 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 5624
Seth Schrock
2,965 Recognized Expert Specialist
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,273 Recognized Expert Top Contributor
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 Recognized Expert Specialist
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,273 Recognized Expert Top Contributor
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 Recognized Expert Specialist
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,273 Recognized Expert Top Contributor
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
10758
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, I want that same debug.print information sent to a form so a user can see what is happening as the data is being processed. How can I do that?
2
12821
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
5880
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
18277
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 what is in the tables (date parameters) before running all the queries. This way, if the data in the tables is not for the time period the user needs...
7
1866
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 for queries from scratch? Private Sub ListFieldsBttn_Click() '********************************************* ' Prompt user for the table name. List...
1
2201
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. Thanks, Al
5
5627
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. In VB 2005 this approach either doesn't work or I need to do it some different way. Of course VB 2005 environment allows to see values right in the...
2
2581
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 of ok but doesn't have intellisense. Documentation shows that editions above Express do have immediate windows. We've looked under Debug, View,...
6
2827
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 your cursor was somewhere in the middle of the text output, then there would be no scrolling. In VS2005, the Immediate Window scrolls regardless of...
5
13742
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 drag&drop on the same page (which was not possible). Now i´ve a new concept of the script, more object oriented. I´ve also commented the whole code so you...
0
7479
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7411
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7669
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7439
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3468
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.