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

How to get DoCmd.TransferText to Recognise Negative Numbers Using "()"?

jbt007
40
Hi All,

System: Access 2010, WinXP

I have a text file I am importing using the following code:
Expand|Select|Wrap|Line Numbers
  1. 'Import text report into tblBVRRaw table using the BVRImp Spec...
  2. DoCmd.TransferText acImportFixed, "BVRImp", "tblBVRRaw", strSourceFile, False
  3.  
It works fine except where ther are negative numbers in the report being imported. I checked the spec to be sure both "()", were included in the column. The number (5,342,892) for example is imported as a null value.

Any suggestions? What is the "CodePage" option? Would that tell Access that () means negative?

Thanks in advance...
Jan 4 '12 #1

✓ answered by ADezii

Wanted to make sure that that was the problem, namely the Data Type Conversion. Once these Negative Numbers are in the Imported Table, as TEXT, in the Format of (xxx...), then they can easily be converted to Negative Numbers, such as: -xxx..., the Data Type of the Fields changed to Number/DOUBLE, and the Format Property set to something such as 0;(0);"Zero";"Null", which will display Negative Numbers within Parenthesis. If you need further assistance, just ask.

14 4233
ADezii
8,834 Expert 8TB
  1. Within the BVRImp Specification, change the Data Type of the Field that may contain Negative Numbers to TEXT, rather than NUMERIC. My guess is that (<Some Value>) is not being interpreted as a Negative Number, either within the Specification, or Import Process itself. If I am correct, you are now getting a Data Type Conversion Error along with a subsequent <TableName>_ImportErrors Table.
  2. Have you tried a Manual Import, bypassing the Specification?
Jan 4 '12 #2
jbt007
40
ADezii - you are correct, are Data Type Conversion Errors for the fields in question. I do not see how changing the data type of the field from Number (Doubel), to Text is going to solve my issue. I need it to be a number. Could you elaborate on your suggestion a bit?
Jan 4 '12 #3
ADezii
8,834 Expert 8TB
Wanted to make sure that that was the problem, namely the Data Type Conversion. Once these Negative Numbers are in the Imported Table, as TEXT, in the Format of (xxx...), then they can easily be converted to Negative Numbers, such as: -xxx..., the Data Type of the Fields changed to Number/DOUBLE, and the Format Property set to something such as 0;(0);"Zero";"Null", which will display Negative Numbers within Parenthesis. If you need further assistance, just ask.
Jan 4 '12 #4
NeoPa
32,556 Expert Mod 16PB
I'm using Access 2003 but I couldn't find any way that would work (IE. Importing negative numbers identified by parentheses). I changed the system settings to recognise (n) as -n but nevertheless such entries were always treated as invalid and dropped. There was also nowhere within the Import Specification where such a setting could be applied. In short, it appears it isn't supported :-(

PS. ADezii is talking about importing the data in as text and subsequently updating it using a query. Probably by importing it into one table first followed by appending it to another using a query to handle the transfer and conversion at the same time.
Jan 5 '12 #5
Mihail
759 512MB
Or manage this before importing by replacing (number) to -number in the original file.

It is just an idea because I post for subscription purpose.
Jan 5 '12 #6
Mariostg
332 100+
I had a similar problem before but in my case, negative numbers were in the form - 13456. i.e. negative sign followed by 7 spaces followed by number. Importing this from a text file would lead to a numeric overflow type error if I remember correctly. Since I was already manipulating the text file line be line to clean other garbage, I wrote a function to clean the negative numbers.

Here is the function that maybe will imspire some people.
Expand|Select|Wrap|Line Numbers
  1. Function FixNegativeNumber(LineToFix As String) As String
  2. 'This is to remove the spaces (-   6.54) to (-6.54) that exist in the DRMIS file.
  3.  
  4.     Dim re As New RegExp
  5.     Dim RetStr As String
  6.     Dim pattern As String
  7.     re.pattern = "(,-)\s+([0-9]+)"
  8.     re.Global = True
  9.  
  10.     If (re.Test(LineToFix) = True) Then
  11.         FixNegativeNumber = re.Replace(LineToFix, "$1$2")
  12.     Else
  13.         FixNegativeNumber = LineToFix
  14.     End If
  15. End Function
  16.  
Jan 5 '12 #7
jbt007
40
All,

Thanks for the great feedback. With y'alls (Ya, a Texan!) help I have created a solution that works. Basic process is as follows:

First create a temp table that is all text fields:
Expand|Select|Wrap|Line Numbers
  1.     strSQL = "CREATE TABLE tblTxtRpt ([JVID] Text(5),[SkpFld] Text(2),[Spec] Text(9),[JVTitle] Text(26), " & _
  2.              "[OBTot] Text(17),[COTot] Text(13),[BTot] Text(16),[CTot_td] Text(16),[ATot_tp] Text(13), " & _
  3.              "[ATot_td] Text(16),[PTot_tg] Text(16),[PTot_wm] Text(16),[VTot_td] Text(13),[PftLos] Text(13), [RecID] AutoIncrement);"
  4.     db.Execute strSQL, dbFailOnError
  5.  
Then import the data in to the temporary table:
Expand|Select|Wrap|Line Numbers
  1.     DoCmd.TransferText acImportFixed, "BVRImp", "tblTxtRpt", strSourceFile, False
  2.  
Next, delete all unwanted records. This would include headding records, subtotals, etc. from the report.

Expand|Select|Wrap|Line Numbers
  1.     'Part 1 - Delete Headers, SubTotals, and other text type records that are not needed in tblTxtRpt...
  2.     strSQL = "DELETE tblTxtRpt.JVID, tblTxtRpt.Spec, tblTxtRpt.SkpFld, tblTxtRpt.JVTitle, tblTxtRpt.OBTot, " & _
  3.              "tblTxtRpt.COTot, tblTxtRpt.BTot, tblTxtRpt.CTot_td, tblTxtRpt.ATot_tp, tblTxtRpt.ATot_td, " & _
  4.              "tblTxtRpt.PTot_tg , tblTxtRpt.PTot_wm, tblTxtRpt.VTot_td, tblTxtRpt.PftLos, tblTxtRpt.RecID " & _
  5.              "From tblTxtRpt " & _
  6.              "WHERE (((tblTxtRpt.JVID) Is Null)) OR (((tblTxtRpt.JVID) Like ""*JC BV*"")) OR " & _
  7.              "(((tblTxtRpt.JVID) Like ""*perio*"")) OR (((tblTxtRpt.JVID) Like ""*----*"")) OR " & _
  8.              "(((tblTxtRpt.JVID) Like ""*COST*"" Or (tblTxtRpt.JVID) Like ""*CODE*"")) OR " & _
  9.              "(((tblTxtRpt.Spec) Like ""*request*"")) OR (((tblTxtRpt.Spec) Like ""*Total*"")) OR " & _
  10.              "(((tblTxtRpt.JVID) Like ""*GRAND*""));"
  11.     'Debug.Print strSQL
  12.     db.Execute strSQL, dbFailOnError
  13.  
  14.     'Part 2 - Remove zero values... Can't do this in one step because "----" will not convert to 0 using CDbl()
  15.     '(Well, technically I quess you could using an iif(), but that is slow.  Two delete queries works just fine...)
  16.     strSQL = "DELETE tblTxtRpt.JVID, tblTxtRpt.Spec, tblTxtRpt.JVTitle, CDbl(Nz([OBTot])) AS vOBTot, " & _
  17.              "CDbl(Nz([COTot])) AS vCOTot, CDbl(Nz([BTot])) AS vBTot, CDbl(Nz([CTot_td])) AS vCTot_td, " & _
  18.              "CDbl(Nz([ATot_tp])) AS vATot_tp, CDbl(Nz([ATot_td])) AS vATot_td, CDbl(Nz([PTot_tg])) AS vPTot_tg, " & _
  19.              "CDbl(Nz([PTot_wm])) AS vPTot_wm, CDbl(Nz([VTot_td])) AS vVTot_td, CDbl(Nz([PftLos])) AS vPftLos " & _
  20.              "From tblTxtRpt " & _
  21.              "WHERE (((CDbl(Nz([OBTot])))=0) AND ((CDbl(Nz([COTot])))=0) AND ((CDbl(Nz([BTot])))=0) AND " & _
  22.              "((CDbl(Nz([CTot_td])))=0) AND ((CDbl(Nz([ATot_tp])))=0) AND ((CDbl(Nz([ATot_td])))=0) AND " & _
  23.              "((CDbl(Nz([PTot_tg])))=0) AND ((CDbl(Nz([PTot_wm])))=0) AND ((CDbl(Nz([VTot_td])))=0) AND " & _
  24.              "((CDbl(Nz([PftLos])))=0));"
  25.     'Debug.Print strSQL
  26.     db.Execute strSQL, dbFailOnError
Next, import the data into the "real" table with an insert query:
Expand|Select|Wrap|Line Numbers
  1.     'Transfer final records form temp table into tblBVRRaw for processing...
  2.     strSQL = "INSERT INTO tblBVRRaw ( JVID, Spec, JVTitle, OBTot, COTot, BTot, CTot_td, ATot_tp, ATot_td, PTot_tg, PTot_wm, VTot_td, PftLos )" & _
  3.              "SELECT tblTxtRpt.JVID, tblTxtRpt.Spec, tblTxtRpt.JVTitle, CDbl(Nz([OBTot],0)) AS vOBTot, CDbl(Nz([COTot],0)) AS vCOTot, " & _
  4.              "CDbl(Nz([BTot],0)) AS vBTot, CDbl(Nz([CTot_td],0)) AS vCTot_td, CDbl(Nz([ATot_tp],0)) AS vATot_tp, CDbl(Nz([ATot_td],0)) AS vATot_td, " & _
  5.              "CDbl(Nz([PTot_tg],0)) AS vPTot_tg, CDbl(Nz([PTot_wm],0)) AS vPTot_wm, CDbl(Nz([VTot_td],0)) AS vVTot_td, CDbl(Nz([PftLos],0)) AS vPftLos " & _
  6.              "From tblTxtRpt " & _
  7.              "ORDER BY tblTxtRpt.RecID;"
  8.     'Debug.Print strSQL
  9.     db.Execute strSQL, dbFailOnError
Finally, drop the temporary table.
Expand|Select|Wrap|Line Numbers
  1.     'Drop the Temp table.
  2.     strSQL = "DROP TABLE tblTxtRpt;"
  3.     db.Execute strSQL, dbFailOnError
  4.  
The "key" was the CDbl() function, which translates (<somenumber>) to negative numbers. It will also translates -<somenumber> to negatives.

Again, thanks for all the comments!
Jan 9 '12 #8
jbt007
40
One Final question...

ADezii - you said "...Within the BVRImp Specification, change the Data Type of the Field that may contain Negative Numbers to TEXT...". I don't seem to have a way of changing the report specification field types. How would one do this? I have uploaded a screen shot of BVRImp spec when I click the edit button...
BVRImp_Spec.jpg

Jan 9 '12 #9
NeoPa
32,556 Expert Mod 16PB
An alternative would be to avoid creating and deleting the table every time, or at all in code, and simply create it once (manually is fine) but clear it of all data after use. This has the same effect without the need for deleting and recreating the table (and I would expect it to run faster too). It also means the design of your project is visible in the database, rather than part of the design being hidden in code.
Jan 9 '12 #10
jbt007
40
NeoPa - The intent IS to keep it the temporary table hidden. I tried to do this using the "hidden" property, but every time an import was done, access would unhide the table. Additionally, in another import (Different report, same temp table) the order of the data is very important, so I added the RecID autonumber field. This allows me to keep the report in exact order as the text file's printed report. The problem with importing multiple times and NOT dropping the table is the AutoNumber field does not get set back to 1 when the data is purged. You have to (as far as I know) drop the table and recreate it to reset the AutoNumber field. If there is a way to keep this table hidden AND reset the AutoNumber field I am all for faster and easier!

This is starting to sound like a new thread so if we need to I can post a new question on this topic.
Jan 9 '12 #11
NeoPa
32,556 Expert Mod 16PB
JBT007:
This is starting to sound like a new thread so if we need to I can post a new question on this topic.
No worries. If that is your requirement then dropping and recreating fits perfectly. Personally, I prefer all the design to be visible and obvious, as it avoids inadvertent errors in development, but your solution fits your requirements just fine it seems. No need to create a new thread just to agree I guess ;-)

PS. To reset numbering you could delete and recreate the AutoNumber field, but that's just an aside.
Jan 9 '12 #12
jbt007
40
NeoPa - I went back and looked at my original post:

How to hide a table that is made with SQL "CREATE TABLE"?

It didn't generate much discussion... If I recall, I tried altering the table to delete the filed, then added the field again and Access STILL remembered the AutoNumber and didn't reset it back to "1". It may be the BFI method, but it seems to be working...
Jan 9 '12 #13
NeoPa
32,556 Expert Mod 16PB
JBT007:
NeoPa - I went back and looked at my original post:
I see nothing in the original post. Perhaps you're referring to your first thread?

I must admit it's not something I use frequently, so I can't be sure, but I thought I remembered it being suggested for that purpose in another thread here recently.
Jan 9 '12 #14
Mariostg
332 100+
@jbt007, if you prefix your table name with usys, it will be a hidden table by default.
Jan 10 '12 #15

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

Similar topics

7
by: pj | last post by:
Why does M$ Query Analyzer display all numbers as positive, no matter whether they are truly positive or negative ? I am having to cast each column to varchar to find out if there are any...
4
by: Teresa | last post by:
I'm trying to import a tab delimited text file. The text file does not have column heading. I can give it column heading if needed. How do I change the defult comma delimited to tab? If I...
3
by: holdemfoldem | last post by:
Hi. I'm new to this board and have a few questions about using the method referred to in the topic of this message. I have manually transferred over 1/2 million records from a text file into my...
11
by: tlyczko | last post by:
Hello Rob B posted this wonderful code in another thread, http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/c84d8538025980dd/6ead9d5e61be85f0#6ead9d5e61be85f0 I could not...
0
by: Chris | last post by:
Hi, I am using the command below for exporting data to text file. One of the query columns has SINGLE number type with decimal ponts as auto. DoCmd.TransferText acExportDelim,...
39
by: Frederick Gotham | last post by:
I have a general idea about how negative number systems work, but I'd appreciate some clarification if anyone would be willing to help me. Let's assume we're working with an 8-Bit signed integer,...
14
PEB
by: PEB | last post by:
Hi all, Yesterday i've tried this command: DoCmd.TransferText acExportDelim, "Comma_SEP", "myquery", "D:\Temp\Temp.txt" And it gave me an Error that can't find "D:\Temp\Temp.txt" This...
1
by: amitk | last post by:
Hello, I'm using Access 2002-2003. My application exports an Access table into a text file and then appends the text file with several othet files to generate a final file XXXX.ftm . ...
0
by: Cuaracao | last post by:
This is my code: Code1: 'DoCmd.OpenTable "tblImport" 'DoCmd.TransferDatabase acExport, "dBASE IV", strOutputDir,acTable,"sel_TblImport", strDbfName & ".dbf", False ' DoCmd.Close acTable,...
10
by: sakurako97 | last post by:
hi, i am trying to find out if it is possible to pass the current recordset of a form to DoCmd.TransferText so i can export it as a .csv I know I can use querydefs etc, but it would be a much...
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: 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
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...
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,...
0
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...
0
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...

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.