473,385 Members | 1,655 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,385 software developers and data experts.

Help Writing Field Names to a String

I am trying to concatenate field names (separated by a comma or some of the delimiter) and write them to record in my database, or store them as a string that I can use in an expression later on. So far I have code that pulls all the field names from a query and writes them to the debug window. But I am stuck after that. I can't figure out how to get the output into another format. Any suggestions would be appreciated.


Expand|Select|Wrap|Line Numbers
  1. Sub Fieldnames()
  2.     Dim Rst As Recordset
  3.     Dim db As Database
  4.     Dim f As Field
  5.     Dim qdfParmQry As QueryDef
  6.         Set db = CurrentDb()
  7.         Set qdfParmQry = db.QueryDefs("qry_1test")
  8.         qdfParmQry("Forms!Occu_formatting!Species") = [Forms]![Occu_formatting]![Species]
  9.         Set Rst = qdfParmQry.OpenRecordset()
  10.     For Each f In Rst.Fields
  11.     Debug.Print f.Name
  12.     Next
  13.     Rst.Close
  14. End Sub
Mar 13 '14 #1

✓ answered by Seth Schrock

You would need a string variable to store the field names.
Expand|Select|Wrap|Line Numbers
  1. Dim strFields As String
Now, just below your Debug.Print line, put this
Expand|Select|Wrap|Line Numbers
  1. strFields = strFields & f.Name & ", "
This will leave you with a comma and a space at then end of the string when you are done, so after your loop, you will probably want to drop those.
Expand|Select|Wrap|Line Numbers
  1. strFields = Left(strFields, Len(strFields) - 2)
You might have to play with the number that you are subtracting to make sure it works, but otherwise it should work.

3 1148
Seth Schrock
2,965 Expert 2GB
You would need a string variable to store the field names.
Expand|Select|Wrap|Line Numbers
  1. Dim strFields As String
Now, just below your Debug.Print line, put this
Expand|Select|Wrap|Line Numbers
  1. strFields = strFields & f.Name & ", "
This will leave you with a comma and a space at then end of the string when you are done, so after your loop, you will probably want to drop those.
Expand|Select|Wrap|Line Numbers
  1. strFields = Left(strFields, Len(strFields) - 2)
You might have to play with the number that you are subtracting to make sure it works, but otherwise it should work.
Mar 13 '14 #2
That worked, thanks!
Mar 13 '14 #3
NeoPa
32,556 Expert Mod 16PB
That works Seth, but an alternative approach is to use :
Expand|Select|Wrap|Line Numbers
  1. strFields = strFields & ", " & f.Name
This has the benefit of being easier to tidy up afterwards using :
Expand|Select|Wrap|Line Numbers
  1. strFields = Mid(strFields, 3)
Mar 14 '14 #4

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

Similar topics

0
by: Pato Secruza | last post by:
Hi everyone! I have a really frustrating error and need help. I’m trying to do a function that gets the properties and names of the fields in a MS Access database using ASP. I haven’t...
13
by: temp | last post by:
Hi all, I know this is easy for you guys but I am not a VB developer. My boss just wanted me to generate a mapping of all the queries in our Access DB. So I managed to create some subroutines to...
3
by: cect425mike | last post by:
I'm trying to design and write a little module (or macro even) for someone in my office, but I'm having some trouble. I have the following database structure (Note: just some sample data,...
3
by: kathyburke40 | last post by:
Odd problem. I have a table in the following format: DocID Question1 Question2 Question3 ------------------------------------------------ 298 1, 2, 3 or 0 Each Question...
5
by: EiEiO | last post by:
Hi All, I am trying to create an Import Form to "Map Fields" for importing many different tables into 1. The form I created has 2 columns of comboboxes ( A - AA) thru (J - JJ). The...
3
by: Billy | last post by:
I do a SELECT * from table command in an ASP page to build a text file out on our server, but the export is not to allow a field name rows of records. The first thing I get is a row with all the...
4
by: Bob | last post by:
Hi all, I've got a table that I've imported and it has junk at the top of the table, so after import I run a delete query to remove the junk lines then I'm left with the field names I want for...
4
by: | last post by:
Given an XML file (dataset.writexml), here is my output (simplified for this posting): <?xml version="1.0" standalone="yes"?> <NewDataSet> <Category> <CategoryId>80</CategoryId>...
12
by: Wayne | last post by:
I have been given the task of rewriting a database that seems as though it has been written by someone with a very basic understanding of Access. Many of the object names and field names in tables...
1
by: Cainlynk | last post by:
I am trying to create a passthrough query in Access using VBA code. I have done this several times before successfully, but in this instance, I have run up against a wall due to a field name that...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
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...
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,...

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.