#85123 - 2002-02-18 08:54 PM
COM and Microsoft Access
|
Bonji
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
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
|
|
Top
|
|
|
|
#85126 - 2002-02-18 09:23 PM
Re: COM and Microsoft Access
|
Shawn
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
|
|
|
|
#85129 - 2002-02-19 03:34 PM
Re: COM and Microsoft Access
|
Bonji
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
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
|
|
|
|
#85132 - 2002-05-09 11:23 PM
Re: COM and Microsoft Access
|
Kdyer
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.
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 ]
|
|
Top
|
|
|
|
#85133 - 2002-05-10 03:08 PM
Re: COM and Microsoft Access
|
Anonymous
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
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
|
|
|
|
#85137 - 2002-05-13 02:12 PM
Re: COM and Microsoft Access
|
Anonymous
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
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
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
|
|
|
|
#85141 - 2002-05-14 12:02 AM
Re: COM and Microsoft Access
|
Anonymous
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
|
|
|
|
Moderator: Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart
|
0 registered
and 302 anonymous users online.
|
|
|