Solved

Concatinating open number of fields & updating a table

Posted on 2009-03-30
2
265 Views
Last Modified: 2012-05-06
Decently I have inherited this on-going project, and need to re-work it unfortunately my bronchial pneumonia is kicking my butt at this time.
I get a file of bounced e-mail, the first 4 fields are fine the way they are the issues for the end user is the rest of the fields starting with field 5 thru whatever. The end user wants the remaining fields to be concatenated into one field. Each bounced e-mail files contains a different number of fields e.g. one file may contain fields 1 thru 19 the next may have fields 1 thru 89.
Is it possible and what would the statement look like to check the file for the maximum number of fields then concatenate fields 5 thru that max number field into one field
The current query looks like:
UPDATE Nolistdev201450 SET TEST_FIELD = IIF(ISNULL([field5]),'',IIF([field5]=' ', '',[field5] +' '))+
IIF(ISNULL([field6]),'',IIF([field6]=' ', '',[field6] +' '))+
IIF(ISNULL([field7]),'',IIF([field7]=' ', '',[field7] +' '))+
IIF(ISNULL([field8]),'',IIF([field8]=' ', '',[field8] +' '))+
IIF(ISNULL([field9]),'',IIF([field9]=' ', '',[field9] +' '))+
IIF(ISNULL([field10]),'',IIF([field10]=' ', '',[field10] +' '))+
IIF(ISNULL([field11]),'',IIF([field11]=' ', '',[field11] +' '))+
IIF(ISNULL([field12]),'',IIF([field12]=' ', '',[field12] +' '))+
IIF(ISNULL([field13]),'',IIF([field13]=' ', '',[field13] +' '))+
IIF(ISNULL([field14]),'',IIF([field14]=' ', '',[field14] +' '))+
IIF(ISNULL([field15]),'',IIF([field15]=' ', '',[field15] +' '))+
IIF(ISNULL([field16]),'',IIF([field16]=' ', '',[field16] +' '))+
IIF(ISNULL([field17]),'',IIF([field17]=' ', '',[field17] +' '))+
IIF(ISNULL([field18]),'',IIF([field18]=' ', '',[field18] +' '));

Since I have several hundred of these files to deal with I would like to load all of the files in a table in a MS Access database as fast & easy as possible. The alternate would be MS Excel spread sheet with it limitations.
I am also open to any suggestions.
0
Comment
Question by:mroberson
2 Comments
 
LVL 11

Accepted Solution

by:
mildurait earned 250 total points
Comment Utility
You can try something like this.
Public Sub TestUpdate()

    sql = GenUpdateStatment() 

    Call RunSqlStatement(sql)

End Sub
 

Public Sub TestSQL((

    Call debug.print(GenUpdateStatement())

End Sub
 

Public Sub RunSqlStatement(byVal sql as string)

    call docmd.setwarnings(false)

    call docmd.runsql(true)

    call docmd.setwarnings(false)

ENd Sub
 
 

Public Function GenUpdateStatment() as string
 

    dim sql as string

    Dim rs As DAO.Recordset

    Dim fld As DAO.Field

    Dim f as string

    dim i as long 

    i=0

    Set rs = CurrentDb.OpenRecordset("SELECT * FROM tblPrices WHERE 1=2")

    For Each fld In rs.Fields

        i = i + 1

        f = fld.name

        if i = 5 then sql = sql & "UPDATE Nolistdev201450 SET TEST_FIELD = IIF(ISNULL([" & f & "]),'',IIF([" & f & "]=' ', '',[" & f & "] +' '))" 

        if i>5 then sql = sql & " & IIF(ISNULL([" & f & "]),'',IIF([" & f & "]=' ', '',[" & f & "] +' '))"         

    Next

    rs.close

    set rs = nothing

    GenUpdateStatement = sql
 

End Function

Open in new window

0
 

Author Closing Comment

by:mroberson
Comment Utility
with some modification was albel to use your code. One of the issues that had to be address that I was not aware of was some of the files come with enpty record or 2 that contain some ascii charter so those jam of the works But thanks for the help
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now