473,379 Members | 1,253 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.

How do I run update Queries before exporting table as text file in VBA

I'm building an automated report and need to be able to update a table with update queries before exporting the text files through my module. I'm still a bit of a noob when it comes to VBA and I've listed my code below at the bottom of this post.

The queries work as planned when ran on test tables seperately but I'm not sure how or where to call them in the VBA module. I tried setting the queries up as public functions and calling them in the export public function but the exported text file still has the wrong format. However, the DET Table is updated by the queries.

How do I get the updated DET table to export after the queries run?

Also, how do I get rid of all the warning pop-ups, i.e. "You're about to change 5497 records. Do you want..."

Expand|Select|Wrap|Line Numbers
  1. 'This query updates the DET table and replaces "000000000000" with "0.00"
  2. Public Function qryReplaceAllZeros()
  3.  
  4. DoCmd.OpenQuery "qryReplaceAllZeros", acViewNormal, acEdit
  5.  
  6. End Function
  7.  
  8. 'This query updates the DET table and replaces all leading zeros
  9. Public Function qryRemoveLeadingZeros()
  10.  
  11. DoCmd.OpenQuery "qryRemoveLeadingZeros", acViewNormal, acEdit
  12.  
  13. End Function
  14.  
  15.  
  16. Public Function CreateTextFile()
  17. 'This function creates a fixed-width text file using the Orders table
  18. 'in CDA Fixed File Import tool. The CHRGAMT field will be Right-aligned
  19.  
  20.  
  21.  Dim strRECTYPE As String * 4 'specifies width of 4 characters
  22.  Dim strPROVNUM As String * 20  'specifies width of 20 characters
  23.  Dim strPCN As String * 50    'specifies width of 50 characters
  24.  Dim strCHRGCODE As String * 12 'specifies width of 12 characters
  25.  Dim strFILLER1 As String * 18 'specifies width of 18 characters
  26.  Dim strCHRQTY As String * 7 'specifies width of 7 characters
  27.  Dim strCHRGAMT As String * 15 'specifies width of 15 characters
  28.  Dim strSRVCDATE As String * 8 'specifies width of 8 characters
  29.  Dim strPROC As String * 8 'specifies width of 8 characters
  30.  Dim strORDERMD As String * 22  'specifies width of 22 characters
  31.  Dim strORDERMDTYPE As String * 2    'specifies width of 2 characters
  32.  Dim strFILLER2 As String * 34 'specifies width of 34 characters
  33.  Dim var1 As Variant
  34.  Dim result As String
  35.  Dim mydb As DAO.Database, myset As DAO.Recordset
  36.  Dim intFile As Integer
  37.  
  38.  
  39.  var1 = ""
  40.  
  41.  
  42.  Set mydb = CurrentDb()
  43.  Set myset = mydb.OpenRecordset("DET_STAR", dbOpenTable)
  44.  
  45.  
  46.  
  47.  intFile = FreeFile
  48.  
  49.  Open "C:\Daily_Files\DET.txt" For Output As intFile
  50.  
  51.  
  52.  
  53.  
  54.  'This section puts the records from the DET table in the text
  55.  'file.
  56.   myset.MoveFirst
  57.   Do Until myset.EOF
  58.       LSet strRECTYPE = myset![RECTYPE] 'Field name in brackets
  59.       LSet strPROVNUM = myset![PROVNUM]
  60.       LSet strPCN = myset![PCN]
  61.       LSet strCHRGCODE = myset![CHRGCODE]
  62.       RSet strCHRQTY = myset![CHRQTY]
  63.       RSet strCHRGAMT = myset![CHRGAMT]
  64.       LSet strSRVCDATE = myset![SRVCDATE]
  65.       LSet strPROC = Nz(myset![PROC], [var1])
  66.       LSet strPROVNUM = Nz(myset![PROVNUM], [var1])
  67.       LSet strORDERMD = Nz(myset![ORDERMD], [var1])
  68.       LSet strORDERMDTYPE = Nz(myset![ORDERMDTYPE], [var1])
  69.       LSet strFILLER2 = Nz(myset![FILLER2], [var1])
  70.  
  71.       'Concatenate all of the variables together as in the following:
  72.       Print #intFile, strRECTYPE & strPROVNUM & strPCN & strCHRGCODE & strFILLER1 & strCHRQTY & strCHRGAMT & strSRVCDATE & strPROC & strORDERMD & strORDERMDTYPE & strFILLER2
  73.       myset.MoveNext
  74.   Loop
  75.  
  76.   Close intFile
  77.   myset.Close
  78.   mydb.Close
  79.  
  80.   MsgBox "Text file has been created!"
  81.  
  82. End Function
  83.  
  84.  
Feb 19 '14 #1
10 1481
zmbd
5,501 Expert Mod 4TB
Also, how do I get rid of all the warning pop-ups, i.e. "
For your update queries, I suggest using the execute method instead. You can set error trapping if the update fails.

Using your stored query, use this:

Expand|Select|Wrap|Line Numbers
  1. '(code omitted)
  2. DIM zDB as DAO.Database
  3. Set zDB = CurrentDB
  4. '
  5. '(code omitted)
  6. '
  7. zDB.execute "qryReplaceAllZeros", dbfailonerror
  8. '
  9. '(code omitted)
  10. '
  11. if not zDB is nothing then Set zDB = Nothing
  12.  
As for the export, is the table/query you are attempting to export in the correct formating/order? If not start with making a simple select query that has the fields in the correct order and formatting. Then use the export method.
Feb 19 '14 #2
ADezii
8,834 Expert 8TB
  1. You may wish to consider a couple of Code Changes that I feel will increase your overall Performance and Efficiency:
    Expand|Select|Wrap|Line Numbers
    1. '********************** CODE HAS BEEN INTENTIONALLY OMITTED **********************
    2. Dim MyDB As DAO.Database
    3. Dim myset As DAO.Recordset
    4.  
    5. Set MyDB = CurrentDb()
    6. Set myset = MyDB.OpenRecordset("DET_STAR", dbOpenForwardOnly)
    7.  
    8. intFile = FreeFile
    9.  
    10. Open "C:\Daily_Files\DET.txt" For Output As intFile
    11.  
    12. 'This section puts the records from the DET table in the Text File.
    13. With myset
    14.   Do Until .EOF
    15.     LSet strRECTYPE = ![RECTYPE] 'Field name in brackets
    16.     LSet strPROVNUM = ![PROVNUM]
    17.     LSet strPCN = ![PCN]
    18.     LSet strCHRGCODE = ![CHRGCODE]
    19.     RSet strCHRQTY = ![CHRQTY]
    20.     RSet strCHRGAMT = ![CHRGAMT]
    21.     LSet strSRVCDATE = ![SRVCDATE]
    22.     LSet strPROC = Nz(![Proc], "")
    23.     LSet strPROVNUM = Nz(![PROVNUM], "")
    24.     LSet strORDERMD = Nz(![ORDERMD], "")
    25.     LSet strORDERMDTYPE = Nz(![ORDERMDTYPE], "")
    26.     LSet strFILLER2 = Nz(![FILLER2], "")
    27.  
    28.     'Concatenate all of the variables together as in the following:
    29.     Print #intFile, strRECTYPE & strPROVNUM & strPCN & strCHRGCODE & strFILLER1 & _
    30.                     strCHRQTY & strCHRGAMT & strSRVCDATE & strPROC & strORDERMD & _
    31.                     strORDERMDTYPE & strFILLER2
    32.       myset.MoveNext
    33.   Loop
    34. End With
    35.  
    36. Close intFile
    37. myset.Close
    38. MyDB.Close
    39. '********************** CODE HAS BEEN INTENTIONALLY OMITTED **********************
    40.  
  2. To omit those annoying Warning Prompts:
    Expand|Select|Wrap|Line Numbers
    1. DoCmd.SetWarnings False
    2.   'Execute Action Query
    3. DoCmd.SetWarnings True     'RESET
Feb 20 '14 #3
zmbd
5,501 Expert Mod 4TB
You shouldn't need ADezii's "set warnings" code using the execute method.

Use ADezii's code, insert the execute method as I've outlined inbetween lines 5 and 6 of his code, (make sure to use the set db code only once (^_^)) Make sure to close and release your objects
Feb 20 '14 #4
NeoPa
32,556 Expert Mod 16PB
I prefer the .Execute() method (over the .OpenQuery() or .RunSQL() methods) for three reasons :
  1. It has an Options parameter where you can pass dbFailOnError to specify the process should have no effect unless all proposed updates are successful.
  2. After running, you can refer to the same object (Database, Table, QueryDef, etc) and show how many rows were updated using .RecordsAffected.
  3. You never have to fiddle with the status of the Warnings, which isn't a big deal but can leave them set incorrectly if ever the code crashes between settings. Also, your code has to know what the existing setting is before returning after completion.

Code example to illustrate last point :
Expand|Select|Wrap|Line Numbers
  1. Private Sub X()
  2.     Call DoCmd.SetWarnings(False)
  3.     ...
  4.     Call Y
  5.     ...
  6.     Call DoCmd.SetWarnings(True)
  7. End Sub
  8.  
  9. Private Sub Y()
  10.     Call DoCmd.SetWarnings(False)
  11.     ...
  12.     Call DoCmd.SetWarnings(True)
  13. End Sub
Everything's fine until returning from Y(). All code after that is running with Warnings=True when it should be Warnings=False.
Feb 20 '14 #5
ADezii
8,834 Expert 8TB
To add what NeoPa and zmbd have already stated:
  1. If you are going to use SetWarnings, you may wish to set this Option to True within an Error Trap, should one occur.
  2. You may want to encapsulate these Queries within a Transaction to ensure that the Data is left in a consistent state prior to writing to the Text File. Either all Actions succeed or none.
Feb 20 '14 #6
zmbd
5,501 Expert Mod 4TB
OK, now to the report, Line 71 and 72 in op.
Build the string first!
Then you can see what is actually happening with resulting return by issing a debug.print command.
Feb 20 '14 #7
I guess I'm more of a noob than I thought. You guys lost me.

ZMBD,

I'm not sure I understand what you mean here...

"You shouldn't need ADezii's "set warnings" code using the execute method.

Use ADezii's code, insert the execute method as I've outlined inbetween lines 5 and 6 of his code, (make sure to use the set db code only once (^_^)) Make sure to close and release your objects
"

I adopted ADezzi's approach and tried adding your code between lines 5 &6 but I'm not sure what you're referencing when you write 'code omitted and I'm not having any luck making it work.

After reading the posts further, I'm confused as to whether the queries should have their own sub or where/how exactly they should be placed in the function.
Feb 20 '14 #8
zmbd
5,501 Expert Mod 4TB
Post your "new" code and we'll go from there (^_^)
Feb 20 '14 #9
zmbd,

I think I figured it out. This seems to have worked like a champ. Please review and let me know if there's something I missed.



Expand|Select|Wrap|Line Numbers
  1. Public Function CreateTextFile1()
  2.  
  3. 'This function creates a fixed-width text file using the Orders table
  4. 'in CDA Fixed File Import tool. The CHRGAMT field will be Right-aligned
  5.  
  6.  
  7.  Dim strRECTYPE As String * 4 'specifies width of 4 characters
  8.  Dim strPROVNUM As String * 20  'specifies width of 20 characters
  9.  Dim strPCN As String * 50    'specifies width of 50 characters
  10.  Dim strCHRGCODE As String * 12 'specifies width of 12 characters
  11.  Dim strFILLER1 As String * 18 'specifies width of 18 characters
  12.  Dim strCHRQTY As String * 7 'specifies width of 7 characters
  13.  Dim strCHRGAMT As String * 15 'specifies width of 15 characters
  14.  Dim strSRVCDATE As String * 8 'specifies width of 8 characters
  15.  Dim strPROC As String * 8 'specifies width of 8 characters
  16.  Dim strORDERMD As String * 22  'specifies width of 22 characters
  17.  Dim strORDERMDTYPE As String * 2    'specifies width of 2 characters
  18.  Dim strFILLER2 As String * 34 'specifies width of 34 characters
  19.  Dim var1 As Variant
  20.  Dim mydb As DAO.Database, myset As DAO.Recordset
  21.  Dim intFile As Integer
  22.  
  23.  
  24.   var1 = ""
  25.  
  26.  Set mydb = CurrentDb()
  27.  Dim zDB As DAO.Database
  28.     Set zDB = CurrentDb
  29.  
  30.     '(code omitted)
  31.     DoCmd.SetWarnings False
  32.  
  33.     zDB.Execute "qryReplaceAllZeros", dbFailOnError
  34.  
  35.     zDB.Execute "qryRemoveLeadingZeros", dbFailOnError
  36.  
  37.     DoCmd.SetWarnings True  'Reset
  38.  If Not zDB Is Nothing Then Set zDB = Nothing
  39.  
  40.  'zDB.Close
  41.  
  42.  
  43.  
  44.  Set myset = mydb.OpenRecordset("DET_STAR", dbOpenForwardOnly)
  45.  
  46.  intFile = FreeFile
  47.  
  48.  Open "C:\Daily_Files\DET.txt" For Output As intFile 
  49.  
  50.  
  51.  'This section puts the records from the DET table in the text
  52.  'file.
  53.   With myset
  54.     Do Until .EOF
  55.       LSet strRECTYPE = myset![RECTYPE] 'Field name in brackets
  56.       LSet strPROVNUM = myset![PROVNUM]
  57.       LSet strPCN = myset![PCN]
  58.       LSet strCHRGCODE = myset![CHRGCODE]
  59.       LSet strCHRQTY = myset![CHRQTY]
  60.       RSet strCHRGAMT = myset![CHRGAMT]
  61.       LSet strSRVCDATE = myset![SRVCDATE]
  62.       LSet strPROC = Nz(myset![PROC], [var1])
  63.       LSet strPROVNUM = Nz(myset![PROVNUM], [var1])
  64.       LSet strORDERMD = Nz(myset![ORDERMD], [var1])
  65.       LSet strORDERMDTYPE = Nz(myset![ORDERMDTYPE], [var1])
  66.       LSet strFILLER2 = Nz(myset![FILLER2], [var1])
  67.  
  68.       'Concatenate all of the variables together as in the following:
  69.       Print #intFile, strRECTYPE & strPROVNUM & strPCN & strCHRGCODE & strFILLER1 & _
  70.                       strCHRQTY & strCHRGAMT & strSRVCDATE & strPROC & strORDERMD & _
  71.                       strORDERMDTYPE & strFILLER2
  72.         myset.MoveNext
  73.     Loop
  74.  
  75.   End With
  76.  
  77.  
  78.   Close intFile
  79.   myset.Close
  80.   mydb.Close
  81.  
  82.   MsgBox "Text file has been created!"
  83.  
  84. End Function
Feb 20 '14 #10
zmbd
5,501 Expert Mod 4TB
Just a really quick look at the code as I'm on my way out the door with the boss in 10 minutes:

Line 26/27/28 this is what I was warning you about.
Either use mydb or zdb but not both to set to CurrentDB

As you already have line20 and 26 with mydb, change all zdb to mydb and remove lines 27 and 28

Also remove Line 30,31,37,
Alter line 33,35,38 and other lines with zdb to mydb
Remove 40

Take line 38 as a template.
It is best practice to set the object to nothing after closing the object take line 79 and 80
so for example the "myset" object closed in line 79, using line 38 as template, should have
Expand|Select|Wrap|Line Numbers
  1.  If Not myset Is Nothing Then Set  myset = Nothing
following it and the same for the object you closed in line 80

Close BEFORE setting the object to nothing or the close method will error and have a slight risk of data loss. (^_^)

The rule of thumb here is that if you Open it Close it, if you set it, clear/release it.

OK, gota run now.
-z
Feb 20 '14 #11

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

Similar topics

2
by: Michael Thomas | last post by:
Hi everyone Do any of you know if this is possible in Access either by using the import tool or by writing a module in VB: I have a database containing information for a retail chain which,...
1
by: nunYa | last post by:
I am trying to use the DoCmd.TransferText method to export an access table to a Tab Delimited Text File. I have put the code behind the onclick event of a button and keep getting the error...
0
by: jpauthement | last post by:
I have an application which searches through a comma-delimited text file which looks similar to this: "012233010","PAMIC 6X8","FA","0.000","0.000" "012233011","PAMIC 8X8","FA","1.000","0.000" ...
3
by: ghadley_00 | last post by:
Hi, I have a MS access database into which I need to import a text file that is oriented vertically (fields are listed from top to bottom with the value for each field appearing to the right of...
0
by: kamadeep | last post by:
Hi I have a text file which contains a set of unique keys. I need to read this text file and for each one of these i need to update some fields on the SQL server table. Seems to be pretty...
5
harshadd
by: harshadd | last post by:
can some one explain me, how to update foxpro table (.dbf file) using clien side coading in DHTML or ay client side code like java?
4
by: grumpydadtl | last post by:
I am exporting a text file from Access that will be fed into a mainframe application (third party - not something I manage). The file contains a header record, body - which is the variable number of...
4
by: CD Tom | last post by:
This is driving me nuts!!! I have a table that one of the columns is set as Double, Fixed, 3 digits. I try and export this to a .txt file for a backup using the docmd.transfertext command but when I...
2
bre1603
by: bre1603 | last post by:
I'm trying to run an UPDATE statement on a table that changes every time the user runs the code. Here's some info: (On a form) The user imports a text file, using a button to open a file browser...
1
by: kkshansid | last post by:
i have a report ready to print and looks fine in print preview but i have to print it on line printer so i require text file only but after exporting to text file format changes. exported file has...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: 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: 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...
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.