Page 1 of 1 1
Topic Options
#85123 - 2002-02-18 08:54 PM COM and Microsoft Access
Bonji Offline
Starting to like KiXtart

Registered: 2001-09-28
Posts: 169
Loc: Virginia
There are several posts on Access, but none that cover the problem I am having. I feel like my code is pretty close and I'm on the verge of success, but I've struggled for awhile now without making that last leap. Basically I'm trying to open a database in Access, and then import a tab-delimited text file and import it into an existing table. I've tried 2 different methods and neither work, but I believe they are failing for the same reason. Here's the code so far... (I'm trying this UBB Code for the first time, I hope it works)

code:

$AC1=CREATEOBJECT("ACCESS.APPLICATION")
IF @ERROR = 0
$AC1.VISIBLE = 1
$AC1.OPENCURRENTDATABASE("C:\DATA\KIX\BRNP\AUTHOR~1\AUTHOR~1.MDB")
;$AC1.DOCMD.RUNMACRO("IMPORT DATA")
$AC1.DOCMD.TRANSFERTEXT("ACIMPORTDELIM","WESDATA1","SERVICES","C:\DATA\KIX\BRNP\AUTHOR~1\SERVICES\TEST2.TXT")
;$A=MESSAGEBOX(@SERROR,@ERROR)
$AC1.USERCONTROL = 1
ELSE
$A=MESSAGEBOX(@SERROR,@ERROR)
ENDIF

Everything in this works expect for the fact that it won't import the file. The macro won't run nor can I get the TRANSFERTEXT function to work. The Macro I have setup in Access works if I run it. I pulled all of the settings for TRANSFERTEXT out of the Macro's settings so I think they're right. I hope someone can shed some light on this for me. Thanks

[ 18 February 2002: Message edited by: Ben Dulaney ]

Top
#85124 - 2002-02-18 09:02 PM Re: COM and Microsoft Access
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Ben,

Have a look at this post..

You should be able to do something similar without importing the file, but rather "reading" it in..

READEXCEL - Read an Excel Spreadsheet and return results

We use this with a Vendor-Supplied COM Object and it works beautiful!

HTH,

- Kent

_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#85125 - 2002-02-18 09:11 PM Re: COM and Microsoft Access
Bonji Offline
Starting to like KiXtart

Registered: 2001-09-28
Posts: 169
Loc: Virginia
I can get KiX to read and modify my file (which I have it do before it gets imported into Access), but if I'm looking at that script correctly, it looks like it exits as soon as a cell is empty. I am working with a tab-delimited text file that has lots of empty fields periodically. I'm sorry if I misread the code and it doesn't do what I just said. More importantly though, I'm trying to learn COM and I don't see where my mistake is in my code and I would like to improve upon my current knowledge. I plan on using COM a great deal in the future so it's important I don't skip any steps. I do appreciate your input. Thanks again.

[ 18 February 2002: Message edited by: Ben Dulaney ]

Top
#85126 - 2002-02-18 09:23 PM Re: COM and Microsoft Access
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Ben,

I used the following comma-delim text file:

First,Last,Member
Shawn,Tassie,173
Ben,Dulaney,3314
kdyer,,1559

with field names on first row, and the following snippet of code (based on your code):

BREAK ON

$DATABASE = "M:\ACCESS\DB1.MDB"
$TABLE = "NEWTABLE"
$TEXTFILE = "M:\ACCESS\TEST2.TXT"

$AC1=CREATEOBJECT("ACCESS.APPLICATION")

$AC1.VISIBLE = 1

$AC1.OPENCURRENTDATABASE($DATABASE)

$AC1.DOCMD.TRANSFERTEXT(,,$TABLE,$TEXTFILE,-1)

?"@SERROR"

$AC1.USERCONTROL = 1

$AC1.QUIT

$AC1 = 0

EXIT 1

Hope this helps

-Shawn

Top
#85127 - 2002-02-18 10:53 PM Re: COM and Microsoft Access
Bonji Offline
Starting to like KiXtart

Registered: 2001-09-28
Posts: 169
Loc: Virginia
It appears my first parameter in TRANSFERTEXT was messing things up. I appreciate the assistance as it is now working for me. Thanks

[ 18 February 2002: Message edited by: Ben Dulaney ]

Top
#85128 - 2002-02-19 12:17 AM Re: COM and Microsoft Access
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
So what does your final statement look like ? I had the realization that the code I posted will only create a table, not append to an existing one (I think) ... did you figure out how to import-append ?

-Shawn

Top
#85129 - 2002-02-19 03:34 PM Re: COM and Microsoft Access
Bonji Offline
Starting to like KiXtart

Registered: 2001-09-28
Posts: 169
Loc: Virginia
The script does append the data into the table. I'm not sure how to make it replace (not my goal here anyway). Here's what I ended up with...

code:

$DATABASE="C:\DATA\KIX\BRNP\AUTHOR~1\AUTHOR~1.MDB"
$TABLE="SERVICES"
$TEXTFILE="C:\DATA\KIX\BRNP\AUTHOR~1\SERVICES\TEST2.TXT"
$SPEC="WESDATA1"

$AC1=CREATEOBJECT("ACCESS.APPLICATION")
IF @ERROR = 0
$AC1.VISIBLE = 1
$AC1.OPENCURRENTDATABASE($DATABASE)
$AC1.DOCMD.TRANSFERTEXT(,$SPEC,$TABLE,$TEXTFILE,0)
$AC1.QUIT
$AC1=0
ELSE
$A=MESSAGEBOX(@SERROR,@ERROR)
ENDIF


Thanks again for your help Shawn.

Top
#85130 - 2002-05-09 03:00 PM Re: COM and Microsoft Access
Anonymous
Unregistered


Shawn,

I have been reading alot of the COM postings, specifically looking for a solution similar to Ben Dulaney.

I have a small Access DB that 2 Nurses carry on a floppy and use with a laptop. This is a simple DB with 1 form and 1 table (not my design). The nurses see patients and input patient specific data via the form and save the DB on the floppy. The nurses give the floppy to a 3rd person who imports the data into the Master database (same form, same table).

I want to be able to APPEND their database info to the MASTER database with a script rather than opening Access and doing it manually.

Thanks in advance. BTW I loved your entry level explanation on COM objects. Where do I find lesson 2?

Top
#85131 - 2002-05-09 05:34 PM Re: COM and Microsoft Access
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Hey Paul, great to see you back on the board.

First off - Im not a DBA so im hoping someone like Kent (kyder) will step in and save me.

Not to make this sound like a big deal or anything, but I think your first step would be to document the manual steps this 3rd person uses to import the floppy data into the master database. That is to say, which tables get opened, what menu items get used and what options do they select, etc,etc

Would suggest that there may be two approaches to this:

1) Write a script that automates Microsoft Access itself (like the scripts above) that basically performs the same steps this 3rd person does, but in an automated fashion.

2) Write and test your own SQL procedure that does what you need done, then use something like ADO to implement this procedure in Kixtart.

-Shawn

Top
#85132 - 2002-05-09 11:23 PM Re: COM and Microsoft Access
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
3MGrant5,

What may need to do is build your query in the Grid Editor and you should come up with something like this for an "Append Query."

You probably don't need the "setwarnings" stuff. [Big Grin]

code:
$AC1.DoCmd.SetWarnings False
$AC1.RUNSQL = "INSERT INTO PhoneDirectory ( Name, Area, Ext, Login, Email ) SELECT DISTINCTROW [All].[Display name], [All].Company, [All].Phone, [All].Alias, [All].Alias FROM [All] LEFT JOIN PhoneDirectory ON [All].Alias = PhoneDirectory.Login WHERE (((PhoneDirectory.Login) Is Null))"
$AC1.DoCmd.SetWarnings True

However, you may need to do some debugging with the query (note the chr(34)'s)..

code:
$AC1.DoCmd.SetWarnings False
$AC1.DoCmd.RunSQL "INSERT INTO ErrorLog ( TStamp, Loan, ErrorMessage ) SELECT Now()," & Chr(34) & loan_nm & Chr(34) & "," & Chr(34) & oDTrac.ErrorMsg & Chr(34) & ";"
$AC1.DoCmd.SetWarnings True

HTH,

- Kent

[ 09 May 2002, 23:25: Message edited by: kdyer ]
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#85133 - 2002-05-10 03:08 PM Re: COM and Microsoft Access
Anonymous
Unregistered


Shawn,

Good to be back. Dude, the piece that you posted about importing a text file is exactly what I needed. However, that is only half of what I need. Can you demonstrate the same concept but inverse? ie.. Exporting

I created a blank "working" database, then exported the table and form from the Master database to the "working" database (definitions only, no data).

After the nurses enter data in their "working" database I need to be able to export those records in the table of the "working" database to a comma-delim text file then I can use your previous code to import that same text file into the same table in the master database.

Top
#85134 - 2002-05-10 08:06 PM Re: COM and Microsoft Access
Anonymous
Unregistered


Ok, guys. OFF TOPIC

It is obvious to me that the software driving this board is newer than when I first started posting. What do I need to do to display this forum "Left Aligned". All of the postings have a horizontal scroll bar. And, all of the postings on this topic (except for Kdyer's) start about half way across the screen.

What am I doing wrong here?

Thanks,

P.G.

Top
#85135 - 2002-05-10 08:25 PM Re: COM and Microsoft Access
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Yeah... the problem starts when someone posts long lines in CODE tags...

The only advice I can offer is to make use of the "Printer-friendly view of this topic" at the bottom of the thread to wrap all but what's between CODE tags.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#85136 - 2002-05-10 08:30 PM Re: COM and Microsoft Access
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
It would be better if those individuals that post the long lines realized the effect it has on the thread and stopped doing it. There's nothing wrong with breaking long lines. I mean, KiX has no problem with a few extra CR/LF. It make for better readability for everyone else.

Kent, you should know better.
p.s. You could still redeem yourself and edit that post

[ 10 May 2002, 20:44: Message edited by: LLigetfa ]
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#85137 - 2002-05-13 02:12 PM Re: COM and Microsoft Access
Anonymous
Unregistered


Hello, All!!!

Can someone fill-in the blanks for me here? Show me how to extract / export the contents of a table in an Access Database to a text file using COM?

Top
#85138 - 2002-05-13 06:59 PM Re: COM and Microsoft Access
Anonymous
Unregistered


Here is a post I found on CramSession

Can you guys make this work with Kix?

Export From Access to HTML

"Export data from Access to .txt, .htm, .xls, etc., using Visual Basic"


Visual Basic, Jerry@AppDC.com
Try this out to export from an Access database to
a text, htm, xls, etc. file. Export a table or a
stored query.
'----------------------
Dim sSQL As String
Dim sFileType As String
Dim sDestPath As String
Dim sExt As String
Dim sTableQuery As String

sFileType = "Text;"
sExt = "txt"
sDestPath = "C:\Temp\"
sTableQuery = "tCustomers"

sSQL = "SELECT * INTO [" & sFileType & _
"DATABASE=" & sDestPath & "].[FileName" & _
sExt & "] " & "FROM [" & sTableQuery & "]"

DB.Execute sSQL, dbFailOnError

'-------------
Try these out too:
sFileType = "HTML Export;"
sExt = "htm"
'---
sFileType = ""Excel 4.0;"
sExt = "xls"
'---Also:
"dBase III;"
"dBase IV;"
"dBase 5.0;"
"Lotus WK1;"
'--- I'm still trying to get it to work using
parameterized stored queries...

Top
#85139 - 2002-05-13 09:52 PM Re: COM and Microsoft Access
Anonymous
Unregistered


OK. Fine! Silence makes people try it themselves!

Here is my code:
code:
$DATABASE = "C:\KixTest\laptop.MDB"
$TRANSFERTYPE = "acExportDelim"
$TABLE = "Decubiti"
$TEXTFILE = "C:\KixTest\laptop.TXT"
$AC1=CREATEOBJECT("ACCESS.APPLICATION")

$AC1.VISIBLE = 0
$AC1.OPENCURRENTDATABASE($DATABASE)
$AC1.DOCMD.TRANSFERTEXT($TRANSFERTYPE,,$TABLE,$TEXTFILE,-1)
?"@SERROR"
$AC1.USERCONTROL = 0
$AC1.QUIT
$AC1 = 0
EXIT 1

When I run this I get error message "Type mismatch". What's up with that?

Top
#85140 - 2002-05-13 10:29 PM Re: COM and Microsoft Access
kholm Offline
Korg Regular
*****

Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
3MGrant5,

TRANSFERTYPE = "acExportDelim"

Is a constant representing a number, the value is only known to Access if the visual basic code
is executed from Access.

The value for "acExportDelim" is 2

You should change the line:
$TRANSFERTYPE = "acExportDelim"

To
$TRANSFERTYPE = 2

Access expects a number as transfer type not a string, that's why you get the "Type mismatch"

-Erik

Top
#85141 - 2002-05-14 12:02 AM Re: COM and Microsoft Access
Anonymous
Unregistered


"KHOLM", Dude!!! Where's your 5-stars!!!

You ROCK!!! (can you sense the enthusiasm?)

You were right on, my man! Thanks! Can you enumerate the rest of the AcTextTransferType's or post a link to where we can get more info?

Here is an excerpt from Microsoft:

**** TransferText Method ****

The TransferText method carries out the TransferText action in Visual Basic.

expression.TransferText(TransferType, SpecificationName, TableName, FileName, HasFieldNames, HTMLTableName, CodePage)
  • expression - Required. An expression that returns one of the objects in the Applies To list.
  • TransferType - Optional AcTextTransferType.

    AcTextTransferType can be one of these constants:
    • acExportDelim
    • acExportDelim
    • acExportFixed
    • acExportHTML
    • acExportMerge
    • acImportDelim default
    • acImportFixed
    • acImportHTML
    • acLinkDelim
    • acLinkFixed
    • acLinkHTML
    If you leave this argument blank, the default constant (acImportDelim) is assumed.
  • SpecificationName - Optional Variant. A string expression that's the name of an import or export specification you've created and saved in the current database.
  • TableName - Optional Variant. A string expression that's the name of the Microsoft Access table you want to import text data to, export text data from, or link text data to, or the Microsoft Access query whose results you want to export to a text file.
  • FileName - Optional Variant. A string expression that's the full name, including the path, of the text file you want to import from, export to, or link to.
  • HasFieldNames - Optional Variant. Use True (1) to use the first row of the text file as field names when importing, exporting, or linking. Use False (0) to treat the first row of the text file as normal data. The default is (False).
  • HTMLTableName - Optional Variant. A string expression that's the name of the table or list in the HTML file that you want to import or link.
  • CodePage - Optional Variant. A Long value indicating the character set of the code page.
  • *** Remarks *** - You can leave an optional argument blank in the middle of the syntax, but you must include the argument's comma. If you leave a trailing argument blank, don't use a comma following the last argument you specify.
  • *** Note *** - You can also use ActiveX Data Objects (ADO) to create a link by using ActiveConnection property for the Recordset object.

    [ 14 May 2002, 00:21: Message edited by: 3MGrant5 ]
  • Top
    #85142 - 2002-05-14 09:47 PM Re: COM and Microsoft Access
    kholm Offline
    Korg Regular
    *****

    Registered: 2000-06-19
    Posts: 714
    Loc: Randers, Denmark
    3MGrant5,

    Thanks for the nice words.

    Instead of supplying a list of values, i will give you a way to find them
    yourself.

    To find constants/properties for COM-objects you can start either Word or
    Excel, and start the Visual basic Editor (Hotkey: ALT+F11)

    In the Visual basic editor select Tools-References, if not selected,
    select Microsoft Access X.X object library.

    Now open the object browser (Hotkey: F2)
    In the top selection field, choose Access instead of <All librarys>

    You can now search or browse for the values you need.

    Of course you can use the same procedure to find properties for other
    registrered COM-objects, not only Microsoft's

    -Erik

    [ 14 May 2002, 21:58: Message edited by: kholm ]

    Top
    Page 1 of 1 1


    Moderator:  Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart 
    Hop to:
    Shout Box

    Who's Online
    0 registered and 302 anonymous users online.
    Newest Members
    Sir_Barrington, batdk82, StuTheCoder, M_Moore, BeeEm
    17886 Registered Users

    Generated in 0.069 seconds in which 0.023 seconds were spent on a total of 12 queries. Zlib compression enabled.

    Search the board with:
    superb Board Search
    or try with google:
    Google
    Web kixtart.org