473,796 Members | 2,599 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Run a query in Access db

Jim
I'm a vb.net newbie.

I have an query that is entirely within an Access database. It's a
simple update query - "usp_Append_tbl _RefNos". It works in Access,

How can I run that query from a vb .net program? I have an vb app that
does update another Access table using a stored procedure but it updates
fields with info from a file.

I've tried the code below and I get an error on the ExecuteNonQuery () -
"ODBC - connection to 'XYZ' failed". But I never reference "XYZ" server
anywhere in my program.
=============== =============== =============== =============== ======
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click

Dim RowsAffected As Integer
Dim AConnect As String = _
"Provider=Micro soft.Jet.OLEDB. 4.0;" & _
"Data Source=C:\ABC.m db;"
Dim AccessConnectio n As New OleDbConnection (AConnect)

Dim AccessCommand As New OleDbCommand
AccessCommand.C ommandText = "usp_Append_tbl _RefNos"
AccessCommand.C ommandType = CommandType.Sto redProcedure
AccessCommand.C onnection = AccessConnectio n
'Dim AccessDataAdapt er As New OleDbDataAdapte r(AccessCommand )
AccessConnectio n.Open()

RowsAffected = AccessCommand.E xecuteNonQuery( )

AccessConnectio n.Close()
AccessConnectio n = Nothing
AccessCommand.D ispose()
AccessCommand = Nothing

End Sub

TIA,

Jim
Jun 13 '07 #1
5 4965
Jim
The problem was that there was a reference to XYZ server in the query in
Access. I can run the query if I make that table local.

Now the problem becomes, how do I setup the connections so that Access
database (below Aconnect) will be able to run the query referencing
another database?

Jim wrote:
I'm a vb.net newbie.

I have an query that is entirely within an Access database. It's a
simple update query - "usp_Append_tbl _RefNos". It works in Access,

How can I run that query from a vb .net program? I have an vb app that
does update another Access table using a stored procedure but it updates
fields with info from a file.

I've tried the code below and I get an error on the ExecuteNonQuery () -
"ODBC - connection to 'XYZ' failed". But I never reference "XYZ" server
anywhere in my program.
=============== =============== =============== =============== ======
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click

Dim RowsAffected As Integer
Dim AConnect As String = _
"Provider=Micro soft.Jet.OLEDB. 4.0;" & _
"Data Source=C:\ABC.m db;"
Dim AccessConnectio n As New OleDbConnection (AConnect)

Dim AccessCommand As New OleDbCommand
AccessCommand.C ommandText = "usp_Append_tbl _RefNos"
AccessCommand.C ommandType = CommandType.Sto redProcedure
AccessCommand.C onnection = AccessConnectio n
'Dim AccessDataAdapt er As New OleDbDataAdapte r(AccessCommand )
AccessConnectio n.Open()

RowsAffected = AccessCommand.E xecuteNonQuery( )

AccessConnectio n.Close()
AccessConnectio n = Nothing
AccessCommand.D ispose()
AccessCommand = Nothing

End Sub

TIA,

Jim
Jun 14 '07 #2
On Thu, 14 Jun 2007 09:16:48 -0400, Jim <jo********@Rem oveThisStuffNet scape.netwrote:

¤ The problem was that there was a reference to XYZ server in the query in
¤ Access. I can run the query if I make that table local.
¤
¤ Now the problem becomes, how do I setup the connections so that Access
¤ database (below Aconnect) will be able to run the query referencing
¤ another database?

You probably need to include the user ID and password in the connection string (or table link) to
the other database. What kind of database are you working with?

Could you post the query?
Paul
~~~~
Microsoft MVP (Visual Basic)
Jun 15 '07 #3
Jim
Paul Clement wrote:
On Thu, 14 Jun 2007 09:16:48 -0400, Jim <jo********@Rem oveThisStuffNet scape.netwrote:

¤ The problem was that there was a reference to XYZ server in the query in
¤ Access. I can run the query if I make that table local.
¤
¤ Now the problem becomes, how do I setup the connections so that Access
¤ database (below Aconnect) will be able to run the query referencing
¤ another database?

You probably need to include the user ID and password in the connection string (or table link) to
the other database. What kind of database are you working with?

Could you post the query?
Paul
~~~~
Microsoft MVP (Visual Basic)

The below query, which resides on my Access(2003) db, references a local
table (tbl_X12) and a Linked MS SQL database on another server
(dbo_THG_KEYV_K EY_VALUES).

My question is, how do I "log on" to the SQL server in my VB program?

=============== =============== =============== ===========
usp_Update_tbl_ ClaimNo query in Access database:

SELECT tbl_X12.ClaimNo , dbo_THG_KEYV_KE Y_VALUES.ELEN_I D,
dbo_THG_KEYV_KE Y_VALUES.KEYV_V ALUE
FROM tbl_X12 INNER JOIN dbo_THG_KEYV_KE Y_VALUES ON tbl_X12.Gwid =
dbo_THG_KEYV_KE Y_VALUES.DOCS_I D
WHERE (((tbl_X12.Clai mNo) Is Null) AND
((dbo_THG_KEYV_ KEY_VALUES.ELEN _ID)=418)) OR (((tbl_X12.Clai mNo) Is Null)
AND ((dbo_THG_KEYV_ KEY_VALUES.ELEN _ID)=468)) OR (((tbl_X12.Clai mNo) Is
Null) AND ((dbo_THG_KEYV_ KEY_VALUES.ELEN _ID)=518));
=============== =============== =============== ============
VB .Net program:

' this works if all tables in the usp_Update_tbl_ THG_ClaimNo are local.
' the problem is one table is on an MS SQL server db
' as of today 6/14/7 09:22 I don't know how to reference it in the
below commands

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim RowsAffected As Integer
Dim AConnect As String = _
"Provider=Micro soft.Jet.OLEDB. 4.0;" & _
"Data Source=C:\837.m db;"
Dim AccessConnectio n As New OleDbConnection (AConnect)

Dim AccessCommand As New OleDbCommand
AccessCommand.C ommandText = "usp_Update_tbl _ClaimNo"
AccessCommand.C ommandType = CommandType.Sto redProcedure
AccessCommand.C onnection = AccessConnectio n
'Dim AccessDataAdapt er As New OleDbDataAdapte r(AccessCommand )
AccessConnectio n.Open()

RowsAffected = AccessCommand.E xecuteNonQuery( )

AccessConnectio n.Close()
AccessConnectio n = Nothing
AccessCommand.D ispose()
AccessCommand = Nothing

End Sub
=============== =============== =============== ============
Thanks in advance.

Jim
Jun 15 '07 #4
On Fri, 15 Jun 2007 11:17:02 -0400, Jim <jo********@Rem oveThisStuffNet scape.netwrote:

¤ The below query, which resides on my Access(2003) db, references a local
¤ table (tbl_X12) and a Linked MS SQL database on another server
¤ (dbo_THG_KEYV_K EY_VALUES).
¤
¤ My question is, how do I "log on" to the SQL server in my VB program?
¤
¤ =============== =============== =============== ===========
¤ usp_Update_tbl_ ClaimNo query in Access database:
¤
¤ SELECT tbl_X12.ClaimNo , dbo_THG_KEYV_KE Y_VALUES.ELEN_I D,
¤ dbo_THG_KEYV_KE Y_VALUES.KEYV_V ALUE
¤ FROM tbl_X12 INNER JOIN dbo_THG_KEYV_KE Y_VALUES ON tbl_X12.Gwid =
¤ dbo_THG_KEYV_KE Y_VALUES.DOCS_I D
¤ WHERE (((tbl_X12.Clai mNo) Is Null) AND
¤ ((dbo_THG_KEYV_ KEY_VALUES.ELEN _ID)=418)) OR (((tbl_X12.Clai mNo) Is Null)
¤ AND ((dbo_THG_KEYV_ KEY_VALUES.ELEN _ID)=468)) OR (((tbl_X12.Clai mNo) Is
¤ Null) AND ((dbo_THG_KEYV_ KEY_VALUES.ELEN _ID)=518));
¤ =============== =============== =============== ============
¤ VB .Net program:
¤
¤ ' this works if all tables in the usp_Update_tbl_ THG_ClaimNo are local.
¤ ' the problem is one table is on an MS SQL server db
¤ ' as of today 6/14/7 09:22 I don't know how to reference it in the
¤ below commands
¤
¤ Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
¤ System.EventArg s) Handles Button1.Click
¤ Dim RowsAffected As Integer
¤ Dim AConnect As String = _
¤ "Provider=Micro soft.Jet.OLEDB. 4.0;" & _
¤ "Data Source=C:\837.m db;"
¤ Dim AccessConnectio n As New OleDbConnection (AConnect)
¤
¤ Dim AccessCommand As New OleDbCommand
¤ AccessCommand.C ommandText = "usp_Update_tbl _ClaimNo"
¤ AccessCommand.C ommandType = CommandType.Sto redProcedure
¤ AccessCommand.C onnection = AccessConnectio n
¤ 'Dim AccessDataAdapt er As New OleDbDataAdapte r(AccessCommand )
¤ AccessConnectio n.Open()
¤
¤ RowsAffected = AccessCommand.E xecuteNonQuery( )
¤
¤ AccessConnectio n.Close()
¤ AccessConnectio n = Nothing
¤ AccessCommand.D ispose()
¤ AccessCommand = Nothing
¤
¤ End Sub
¤ =============== =============== =============== ============
¤ Thanks in advance.
You would probably need to reference the table directly by specifying the connection string
information:

[ODBC;Driver={SQ L Server};Server= (local);Databas e=Northwind;Tru sted_Connection =yes].[Table1]

Of course if you're not using a trusted connection you would replace this information with the user
ID and password.
Paul
~~~~
Microsoft MVP (Visual Basic)
Jun 21 '07 #5
Jim
Paul Clement wrote:
On Fri, 15 Jun 2007 11:17:02 -0400, Jim <jo********@Rem oveThisStuffNet scape.netwrote:

¤ The below query, which resides on my Access(2003) db, references a local
¤ table (tbl_X12) and a Linked MS SQL database on another server
¤ (dbo_THG_KEYV_K EY_VALUES).
¤
¤ My question is, how do I "log on" to the SQL server in my VB program?
¤
¤ =============== =============== =============== ===========
¤ usp_Update_tbl_ ClaimNo query in Access database:
¤
¤ SELECT tbl_X12.ClaimNo , dbo_THG_KEYV_KE Y_VALUES.ELEN_I D,
¤ dbo_THG_KEYV_KE Y_VALUES.KEYV_V ALUE
¤ FROM tbl_X12 INNER JOIN dbo_THG_KEYV_KE Y_VALUES ON tbl_X12.Gwid =
¤ dbo_THG_KEYV_KE Y_VALUES.DOCS_I D
¤ WHERE (((tbl_X12.Clai mNo) Is Null) AND
¤ ((dbo_THG_KEYV_ KEY_VALUES.ELEN _ID)=418)) OR (((tbl_X12.Clai mNo) Is Null)
¤ AND ((dbo_THG_KEYV_ KEY_VALUES.ELEN _ID)=468)) OR (((tbl_X12.Clai mNo) Is
¤ Null) AND ((dbo_THG_KEYV_ KEY_VALUES.ELEN _ID)=518));
¤ =============== =============== =============== ============
¤ VB .Net program:
¤
¤ ' this works if all tables in the usp_Update_tbl_ THG_ClaimNo are local.
¤ ' the problem is one table is on an MS SQL server db
¤ ' as of today 6/14/7 09:22 I don't know how to reference it in the
¤ below commands
¤
¤ Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
¤ System.EventArg s) Handles Button1.Click
¤ Dim RowsAffected As Integer
¤ Dim AConnect As String = _
¤ "Provider=Micro soft.Jet.OLEDB. 4.0;" & _
¤ "Data Source=C:\837.m db;"
¤ Dim AccessConnectio n As New OleDbConnection (AConnect)
¤
¤ Dim AccessCommand As New OleDbCommand
¤ AccessCommand.C ommandText = "usp_Update_tbl _ClaimNo"
¤ AccessCommand.C ommandType = CommandType.Sto redProcedure
¤ AccessCommand.C onnection = AccessConnectio n
¤ 'Dim AccessDataAdapt er As New OleDbDataAdapte r(AccessCommand )
¤ AccessConnectio n.Open()
¤
¤ RowsAffected = AccessCommand.E xecuteNonQuery( )
¤
¤ AccessConnectio n.Close()
¤ AccessConnectio n = Nothing
¤ AccessCommand.D ispose()
¤ AccessCommand = Nothing
¤
¤ End Sub
¤ =============== =============== =============== ============
¤ Thanks in advance.
You would probably need to reference the table directly by specifying the connection string
information:

[ODBC;Driver={SQ L Server};Server= (local);Databas e=Northwind;Tru sted_Connection =yes].[Table1]

Of course if you're not using a trusted connection you would replace this information with the user
ID and password.
Paul
~~~~
Microsoft MVP (Visual Basic)

I THINK I understand... but No, I don't...

I have this connection string to my Access database
Dim AConnect As String = _
"Provider=Micro soft.Jet.OLEDB. 4.0;" & _
"Data Source=C:\837.m db;"

How do I How do I integrate your suggestion into that?

Jim
Jun 25 '07 #6

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

Similar topics

3
23628
by: Robert | last post by:
I am having performance issues on a SQL query in Access. My query is accessing and joining several tables (one very large one). The tables are linked ODBC. The client submits the query to the server, separated by several states. It appears the query is retrieving gigs of data from the table and processing the joins on the client. Is there away to perform more of the work on the server there by minimizing the amount of extraneous table...
8
6463
by: Adam Louis | last post by:
I would like help resolving this problem. I'm a novice who's been hired to query a hospital database and extract useful information, available to me only in a dynamically generated, downloadable .mdb. The query below query runs correctly and without error, but any attempt to save it causes Access to crash without a message, leaving the .ldb file. Opening the DB reveals it saved a blank "query1". I've upgraded to Jet SP 8, and I'm running...
12
2323
by: Joe Stanton | last post by:
Hello Group I have a query that works in Oracle and SQL Server, but fails in Microsoft Access. The query is: SELECT data fromTABLE1 WHERE data>='A&' AND data<'A''' Here is my sample data:
6
4852
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID SalesManName AT Alan Time
22
31213
by: Stan | last post by:
I am working with Access 2003 on a computer running XP. I am new at using Access. I have a Db with a date field stored as mm/dd/yyyy. I need a Query that will prompt for the month, ie. 6 for June, and will return all records in that month.
11
16335
by: funky | last post by:
hello, I've got a big problem ad i'm not able to resolve it. We have a server running oracle 10g version 10.1.0. We usually use access as front end and connect database tables for data extraction. We have been using oracle client 10.1.0.2 with it's odbc for a while without problem. The problem arose when we decided to reconnect all the tables and save password. Some query became suddenly very slow. Then I've discovered that the tables...
3
7774
by: mnjkahn via AccessMonster.com | last post by:
I'm running Access 2003, modifying a query that has over 45 fields. When I right click on the field name in Query Design View, and then click Build, Access crashes before the Build window appears. It doesn't happen every time, and using the Zoom window works fine. It appears that it only happens when I want to modify an existing expression. This continues to happen even after the database is repaired and reopened. Anyone have any...
6
4410
by: jsacrey | last post by:
Hey everybody, got a secnario for ya that I need a bit of help with. Access 97 using linked tables from an SQL Server 2000 machine. I've created a simple query using two tables joined by one field between them. The join field in both tables are indexed and I'm selecting 1 field from each table to lookup. The Access query is taking more than 60 second to retrieve 1 record and if I execute the same query within the Query Analyzer, it...
2
9845
by: existential.philosophy | last post by:
This is a new problem for me: I have some queries that open very slowly in design view. My benchmark query takes about 20 minutes to open in design view. That same query takes about 20 minutes to open in datasheet view. As an experiment, I deleted all rows in all tables; after that, the query took only seconds to open in both design view and datasheet view. From these facts, I conclude that Access is evaluating the query when I go to...
9
3637
by: Sinner | last post by:
Hi, I have a field name 'USER' in tableMAIN. How do I replace the user names with corresponding user names. I can do that in xl using vlookup but now I'm trying to find a way to do that in access. For a user mismatch, it should give NA Thx.
0
9533
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10190
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10019
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9057
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6796
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4122
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.