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

Adding records to MS Access table from command line

Hi,
I have text files that are created with comma-delimited records inside them. I am trying to figure out how I can construct a command-line method of importing the records to a MSA table. The end result will be that I can right-click on the .txt file, there will be an option on the context menu that says something like "Add records to 'Modules' table", and that will bring up a command window that will execute the import command. I have no problem with the context menu (and working with the registry), but I'm not very familiar with the macros, specifically - how would I transfer the file name from the command line to the macro in MSA? Would anyone know of a better method?
Mar 9 '10 #1
10 10073
TheSmileyCoder
2,322 Expert Mod 2GB
Im unsure how that would be done, but could you not just open a filebrowser from within access then select teh files to import? Or create a form showing the directory structure, with import buttons?
Mar 9 '10 #2
The text files are generated by another program, and they are constantly being created. I would like to be able to add the records from the text file without having to manually open MS Access.
Mar 9 '10 #3
I think I have a solution but it's a bit of a mess, and it only works if the incoming files are put into the same folder every time

Use a batch file to rename all files within the incoming directory to a standardized format.

Use the same batch file to open an access database where you have set the startup (under tools) to a form with a single field that has on Gotfocus set to run a macro. (I am sort of new to access and I am unsure if there is another way to run a macro on startup.)

Within the macro use the Transfertext action and in the settings use Import Delimited and set the file name to the standardized file name set with the batch file.

Add the Runcommand action with Exit as the command to your macro if you just want to import the data and nothing else.

You can now either manually move the imported files to an archive folder or add a line in the batch file to do so. (to avoid the macro pulling in old files)

I know this still opens access, but takes away the need to manually import the data and cleans up when it's done.
Mar 9 '10 #4
Ok. I understand what you are doing with the import text command with the macro, and there is a way to run it at startup from a command prompt. The only thing I am looking for is how to pass the name of the file into the macro, such as this manner:

c:\>"C:\Program Files\Microsoft Office\Office\msaccess.exe" "C:\Program Files\Microsoft Office\Office\samples\northwind.mdb" /X AddProducts

The above command looks for the msaccess.exe executable, then opens the northwind.mdb database and runs a macro (/X switch) called "AddProducts".
But what if I want to pass it a text field, such as one that happens to contain an absolute path to a file, such as "c:\temp\newdata.txt"? That way I could work with the file from within the macro.
Mar 10 '10 #5
ADezii
8,834 Expert 8TB
  1. Create a Batch File that accepts a Replaceable Parameter representing the Path to the File.
  2. Create an Environmental Variable equal to the Replaceable Parameter.
  3. Within the Batch File, Open the Database within Microsoft Access and automatically execute the AddProducts Macro on Open.
  4. The AddProducts Macro will execute a Public Function that will read the Value of the newly created Environmental Variable using Environ() Function.
  5. Execute the Import Code via VBA now that you have the File's location.
  6. Sample Batch File (Import.bat):
    Expand|Select|Wrap|Line Numbers
    1. @echo off
    2. cls
    3. Set FILE_PATH =  %1
    4. "C:\Program Files\Microsoft Office\Office10\Msaccess.exe" "C:\Program Files\Microsoft Office\Office10\Samples\Northwind.mdb" /X AddProducts
  7. Batch File Execution Sample:
    Expand|Select|Wrap|Line Numbers
    1. Import C:\Stuff\MyFile.txt
  8. This is only Theory and hasn't actually been tested, however, I see no reason whatsoever why it shouldn't work.
Mar 10 '10 #6
NeoPa
32,556 Expert Mod 16PB
Invoking a Database From the Command Line may provide some general answers that could be used for this.
Mar 10 '10 #7
ADezii, That sounds like a great idea!
I think that will work, and I think it's what I've been waiting for. You really put some good ideas together with the environmental variable concept. I am absolutely blown away! Many thanks!
Mar 10 '10 #8
NeoPa
32,556 Expert Mod 16PB
There is no need to create an Environment Variable to pass a value into an Access database instance. Check out the link in my last post for full details.

Alternatively, there's no reason it wouldn't work for you. Just not necessary really.
Mar 10 '10 #9
Thanks, NeoPa. I didn't see that part about passing in the variable.
Mar 10 '10 #10
NeoPa
32,556 Expert Mod 16PB
Not a problem Will. We all have different sets of knowledge and experience. Also, there's probably a lot in there to take in with a quick browse.
Mar 10 '10 #11

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

Similar topics

6
by: Jamie Fryatt | last post by:
Hi everyone, here's what id like to do. I have a table with 2 fields, name and value I need to be able to add multiple records quickly, for example I need to add name value abc 1...
4
by: Manish Sawjiani | last post by:
Hi Experts, I am just into dot net and i want simple code for adding records into Access table (97) using oledb. There are no queries in the databases and there is only one table: Friends with...
7
by: Mathew Hill | last post by:
I am a beginner to the more technical aspects of Microsoft Access (2000) and was wondering if any one can help. I have 3 buttons on a form which add, delete and search for a record. However, when I...
6
by: Robin S. | last post by:
**Eric and Salad - thank you both for the polite kick in the butt. I hope I've done a better job of explaining myself below. I am trying to produce a form to add products to a table (new...
1
by: RC | last post by:
I have an Access 2002 database with many tables and forms (but just to keep things simple, let's say the DB has one Table "Table1" and one Form "Form1"). I have managed to cobble together so much...
6
by: Rudy | last post by:
Hi all, I know this is easy, just can't seem to get it. I have a windows form, and a text box, with a value already in it. I need to add that value to a table. It's just one value, so the entire...
1
by: vbDavidC | last post by:
I am adding a new record to a table via a dataset/adapter. I have got the following to work for me but I am wondering if there is a better way to do this. I am having to have something in my...
0
by: EricLondaits | last post by:
Hi, I have an ASP.NET page with a ListBox that is data bound to a table with a single field (it holds a list of valid IDs). The page also has a textBox into which you can add new valid IDs, one...
2
by: altesse33 | last post by:
I have a command button on the main form of my database that allows users to add new records. But, even though new records get added to the sub table when I click on the button, those new records...
2
by: Steve | last post by:
I have zero experience with ODBC. If I have an Access frontend connected to a SQL Database using ODBC, are the tables connected like a frontend/backend Access database where the the tables you see...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.