473,403 Members | 2,183 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,403 software developers and data experts.

Need to Open a Connection from "inside" a Database?

rm
When using ADO in a module inside an Access 2003 database do I need to
open a db connection even though I am working with a table inside the
same database?

I am writing a bit of code to periodically import data from a text
file to a table in a database. The capabilities of the import wizard
are not sufficient to support what we need for manipulating/parsing
the ascii text file.
Dec 6 '07 #1
5 2502
rm wrote:
When using ADO in a module inside an Access 2003 database do I need to
open a db connection even though I am working with a table inside the
same database?

I am writing a bit of code to periodically import data from a text
file to a table in a database. The capabilities of the import wizard
are not sufficient to support what we need for manipulating/parsing
the ascii text file.
There is a connection available for you

CurrentProject.Connection

Most around here would probably recommend DAO over ADO.

--
Roy-Vidar
Dec 6 '07 #2
rm
On Dec 6, 10:43 am, rm <gro...@rlmoore.netwrote:
When using ADO in a module inside an Access 2003 database do I need to
open a db connection even though I am working with a table inside the
same database?

I am writing a bit of code to periodically import data from a text
file to a table in a database. The capabilities of the import wizard
are not sufficient to support what we need for manipulating/parsing
the ascii text file.
Here is the code...

Sub Import()

Dim fso As New Scripting.FileSystemObject
Dim tsIn As Scripting.TextStream
Dim InputFile As String
Dim cmd1 As ADODB.Command

'Values from the file
Dim strX As String
Dim strY As String
Dim strZ As String

InputFile = "C:\temp\exp.txt"

' Make sure the file exists
If Not fso.FileExists(InputFile) Then
MsgBox "Cannot find input file."
Exit Sub
Else
'Open the input file.
Set tsIn = fso.OpenTextFile(InputFile)
Set cmd1 = New ADODB.Command
cmd1.CommandType = adCmdText

'################################################# #####
' THE FOLLOWING LINE THROWS AN ERROR MESSAGE
'Run-Time error '-2147467259 (80004005)
'The database has been placed in a state by user 'userid' on
machine 'machine-id'
'that prevents it from being opened or locked.
'################################################# #####

cmd1.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;
Data Source= p:\mydb.mdb"

'Loop through input file. When at Endof Stream becomes
'True, you've reached the end of the file
Do While Not tsIn.AtEndOfStream
buf = tsIn.ReadLine

'if we have a social security number - first 9 #s numeric
' then write record
If IsNumeric(Mid(buf, 1, 9)) Then
strX = Trim(Mid(buf, 1, 9))
strY = Trim(Mid(buf, 11, 25))
strZ = Trim(Mid(buf, 39, 2))
cmd1.CommandText = "INSERT INTO table1(x, y, z)
values('" & strX & "','" & strY & "','" & strZ & "')"

'################################################# #####
' THE FOLLOWING LINE THROWS AN ERROR MESSAGE
' BECAUSE I DO NOT HAVE A CONNECTION TO A DB
'################################################# #####

cmd1.Execute
End If
Loop

'Close the file
tsIn.Close

'Release object references
Set tsIn = Nothing
Set cmd1 = Nothing
Set fso = Nothing

End If
End Sub
Dec 6 '07 #3
rm
On Dec 6, 11:11 am, RoyVidar <roy_vidarNOS...@yahoo.nowrote:
rm wrote:
When using ADO in a module inside an Access 2003 database do I need to
open a db connection even though I am working with a table inside the
same database?
I am writing a bit of code to periodically import data from a text
file to a table in a database. The capabilities of the import wizard
are not sufficient to support what we need for manipulating/parsing
the ascii text file.

There is a connection available for you

CurrentProject.Connection

Most around here would probably recommend DAO over ADO.

--
Roy-Vidar
Sir,

Thank you for the response.

CurrentProject.Connection throws the same error message. - Run-time
error '-2147467256 (80004005)' The database has been placed in a state
by user 'userid' on machine 'machineid' that prevents it from being
opened or locked.

Am I using the connection correctly?

cmd1.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=" & CurrentProject.Connection

Dec 6 '07 #4
rm
On Dec 6, 10:43 am, rm <gro...@rlmoore.netwrote:
When using ADO in a module inside an Access 2003 database do I need to
open a db connection even though I am working with a table inside the
same database?

I am writing a bit of code to periodically import data from a text
file to a table in a database. The capabilities of the import wizard
are not sufficient to support what we need for manipulating/parsing
the ascii text file.
Got it...

cmd1.ActiveConnection = CurrentProject.Connection

Thank you Roy-Vidar!
Dec 6 '07 #5
rm wrote:
On Dec 6, 11:11 am, RoyVidar <roy_vidarNOS...@yahoo.nowrote:
>rm wrote:
>>When using ADO in a module inside an Access 2003 database do I need to
open a db connection even though I am working with a table inside the
same database?
I am writing a bit of code to periodically import data from a text
file to a table in a database. The capabilities of the import wizard
are not sufficient to support what we need for manipulating/parsing
the ascii text file.
There is a connection available for you

CurrentProject.Connection

Most around here would probably recommend DAO over ADO.

--
Roy-Vidar

Sir,

Thank you for the response.

CurrentProject.Connection throws the same error message. - Run-time
error '-2147467256 (80004005)' The database has been placed in a state
by user 'userid' on machine 'machineid' that prevents it from being
opened or locked.

Am I using the connection correctly?

cmd1.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=" & CurrentProject.Connection
Try

Set cmd1.ActiveConnection = CurrentProject.Connection

--
Roy-Vidar
Dec 6 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: David Furey | last post by:
Hi I have an XML documnet and a XSLT document as shown below THe XSLT document brings back a filtered docmument that has the VendorName that starts with a particular sub-string This works as...
11
by: Pete Wilson | last post by:
Hi folks -- The page at http://www.pwilson.net/submit-demo.html will not validate. The validator at http://validator.w3.org tells me I can't have an input inside a form. Would some kind...
2
by: Joe Lester | last post by:
Does anyone know if there's a way to embed the Postgres database inside an application... so that it runs transparently, without the user even having to know it's there? I would prefer to use...
10
by: qazmlp | last post by:
There are some blocks of C/C++ code put under #if 0 #end if Is there anyway to make the code inside these blocks to get executed (may be by using some command line options)?
12
by: G Patel | last post by:
I've seen some code with extern modifiers in front of variables declared inside blocks. Are these purely definitions (no definition) or are they definitions with static duration but external...
2
by: Mehdi | last post by:
Hi, I need to pass an URL via a hidden value as follow: <input type="hidden" id="Test" runat="Server"> and on Page_Load I assign a value to this hidden input as follow: Test.Value =...
3
by: Peter Proost | last post by:
Hi group, I've got this bit of code (see below) which draws a basketball field in a picturebox (width:198, height:368) but now I was wondering what would be the easiest way to check inside the...
0
by: Angrez Singh | last post by:
Hi, I am trying to use a "customcontrol" inside a "webusercontrol" but facing problem with the viewstate of the "customcontrol". When I use the "customcontrol" on a page either adding it...
1
by: vbfoobar | last post by:
Hello, I have HTML input to which I apply some changes. Feature 1: ======= I want to tranform all the text, but if the text is inside an "a href" tag, I want to leave the text as it is. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.