473,513 Members | 6,210 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Running a stored procedure from Access to populate a local table

18 New Member
Hi I have created a VB script in Access which runs a stored procedure which retrieves a dataset and I can display it in an Access form.

The records can be displayed but cannot be modified so I am looking to populate a local Access table with the data.

This is the VB that I am using and I require your help to get it to populate the local table?

Dim cn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim rs As ADODB.Recordset
Dim SQLCmd As String

Set cmd = New ADODB.Command
Set rs = New ADODB.Recordset

cn.connectionString = "provider=msdatashape;data provider=SQLOLEDB;Server=123.456.789.012;DATABASE= My_DB;UID=ABCD;PWD=1234;"
cn.Open

With cmd
.ActiveConnection = cn
.CommandText = "SEL_Suppliers_Summary"
.CommandType = adCmdStoredProc
.Parameters.Refresh
End With

With rs
.ActiveConnection = cn
.CursorType = adOpenForwardOnly
.CursorLocation = adUseServer
End With

Set rs = cmd.Execute
Set Me.Recordset = rs


***I WANT TO CAPTURE THE RECORD SET HERE AND ENTER IT IN TO AN ACCESS TABLE NAMED T_Suppliers***


rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing

errbox:
If Err.Number > 0 Then

MsgBox Err.Description & " " & Err.Number
Exit Sub

End If
Sep 27 '16 #1
7 2722
jforbes
1,107 Recognized Expert Top Contributor
This would be pretty much automatic if you connected Access to SQL through ODBC. I would look at this article: Import or link to SQL Server data. Specifically "Link to SQL Server data", about half way down the page. By going this route, you won't need any VBA code in Access and it will work with the SQL Table Natively with live SQL Data.
Sep 27 '16 #2
madmax262
18 New Member
I am very familiar with connecting an Access frontend to a SQL backend via ODBC. The reason I want to avoid this is because the backend is hosted with an ISP and the Access frontend is local on PCs'.

Access would want to open the whole table and download it to the work station which is not an option as the Internet may drop resulting in a corrupted db. Hence the reason that I want to run a SP and return only 1 or a limited amount of records for processing locally.
Sep 27 '16 #3
jforbes
1,107 Recognized Expert Top Contributor
Given that you have the RecordSet opening, this is roughly how I would go about cloning the Data:
Expand|Select|Wrap|Line Numbers
  1. Dim sSQL As String
  2. ...
  3. Set rs = cmd.Execute
  4.  
  5. ' Loop Through the SQL Records and Insert into the Local Table
  6. While Not rs.EOF
  7.     sSQL = ""
  8.     sSQL = sSQL & "INSERT INTO T_Suppliers ("
  9.     sSQL = sSQL & "  Field1"
  10.     sSQL = sSQL & ", Field2"
  11.     sSQL = sSQL & ", Field3"
  12.     sSQL = sSQL & ") VALUES ("
  13.     sSQL = sSQL & "  '" & NZ(rs!Field1, "") & "' "
  14.     sSQL = sSQL & ", '" & NZ(rs!Field2, "") & "' "
  15.     sSQL = sSQL & ", '" & NZ(rs!Field3, "") & "' "
  16.     sSQL = sSQL & ")"
  17.  
  18.     CurrentDB.Execute sSQL, dbFailOnError
  19.     rs.MoveNext
  20. Wend
Sep 27 '16 #4
madmax262
18 New Member
I added this code but it errors with Runtime error '3709':

This connection cannot be used to perform this operation. It is either closed or invalid in this context.


The code now looks like: -

Dim cn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim rs As ADODB.Recordset
Dim SQLCmd As String
Dim sSQL As String

Set cmd = New ADODB.Command
Set rs = New ADODB.Recordset

cn.connectionString = "provider=msdatashape;data provider=SQLOLEDB;Server=123.456.789.012;DATABASE= My_DB;UID=ABCD;PWD=1234;"
cn.Open


Set rs = cmd.Execute

While Not rs.EOF

sSQL = ""
sSQL = sSQL & "INSERT INTO T_Suppliers ("
aSQL = aSQL & " Supplier_No"
aSQL = aSQL & ", Supplier_Code"
aSQL = aSQL & ", Supplier"
aSQL = aSQL & ", Branch_No"
aSQL = aSQL & ", Contact"
aSQL = aSQL & ", Address_1"
aSQL = aSQL & ", Address_2"
aSQL = aSQL & ", Address_3"
aSQL = aSQL & ", Address_4"
aSQL = aSQL & ", Address_5"
aSQL = aSQL & ", Phone_No"
aSQL = aSQL & ", Fax_No"
aSQL = aSQL & ", Credit_Limit"
aSQL = aSQL & ", Account_Status_No"
aSQL = aSQL & ", Active_Record"
aSQL = aSQL & ", Entered_By"
aSQL = aSQL & ", Entered_Date"
aSQL = aSQL & ", Entered_IP_Address"
aSQL = aSQL & ", Modified_By"
aSQL = aSQL & ", Modified_Date"
aSQL = aSQL & ", Modified_IP_Address"
aSQL = aSQL & ") VALUES ("
aSQL = aSQL & " '" & Nz(Supplier_No, "") & "' "
aSQL = aSQL & ", '" & Nz(Supplier_Code, "") & "' "
aSQL = aSQL & ", '" & Nz(Supplier, "") & "' "
aSQL = aSQL & ", '" & Nz(Branch_No, "") & "' "
aSQL = aSQL & ", '" & Nz(Contact, "") & "' "
aSQL = aSQL & ", '" & Nz(Address_1, "") & "' "
aSQL = aSQL & ", '" & Nz(Address_2, "") & "' "
aSQL = aSQL & ", '" & Nz(Address_3, "") & "' "
aSQL = aSQL & ", '" & Nz(Address_4, "") & "' "
aSQL = aSQL & ", '" & Nz(Address_5, "") & "' "
aSQL = aSQL & ", '" & Nz(Phone_No, "") & "' "
aSQL = aSQL & ", '" & Nz(Fax_No, "") & "' "
aSQL = aSQL & ", '" & Nz(Credit_Limit, "") & "' "
aSQL = aSQL & ", '" & Nz(Account_Status_No, "") & "' "
aSQL = aSQL & ", '" & Nz(Active_Record, "") & "' "
aSQL = aSQL & ", '" & Nz(Entered_By, "") & "' "
aSQL = aSQL & ", '" & Nz(Entered_Date, "") & "' "
aSQL = aSQL & ", '" & Nz(Entered_IP_Address, "") & "' "
aSQL = aSQL & ", '" & Nz(Modified_By, "") & "' "
aSQL = aSQL & ", '" & Nz(Modified_Date, "") & "' "
aSQL = aSQL & ", '" & Nz(Modified_IP_Address, "") & "' "
aSQL = aSQL & ")"

CurrentDb.Execute sSQL, dbFailOnError
rs.MoveNext

Wend

rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing

errbox:
If Err.Number > 0 Then

MsgBox Err.Description & " " & Err.Number
Exit Sub

End If
Sep 28 '16 #5
jforbes
1,107 Recognized Expert Top Contributor
I'm not familiar with that error and Google and Microsoft seems to be a bit ambiguous about the causes.

One possible cause is that the database connection is probably closed when the code doesn't expect it. Where the other is Indexing on a Memo Field.

It might be easer if you could debug it a little and try to determine what line is causing the error.
Sep 29 '16 #6
madmax262
18 New Member
I managed to get it to work using this code: -

Dim cn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim rs As ADODB.Recordset
Dim SQLCmd As String
Dim sSQL As String

Set cmd = New ADODB.Command
Set rs = New ADODB.Recordset

cn.connectionString = "provider=msdatashape;data provider=SQLOLEDB;Server=123.456.789.012;DATABASE= My_DB;UID=ABCD;PWD=1234;"
cn.Open

Set rs = cmd.Execute
Set Me.Recordset = rs

Do Until rs.EOF

CurrentDb.Execute "INSERT INTO T_Suppliers(Supplier_No, Supplier_Code, Supplier, Address_1, Address_2, Address_3, Address_4, Address_5, Phone_No, Fax_No, Credit_Limit, Account_Status_No, Active_Record, Entered_By, Entered_Date, Entered_IP_Address, Modified_By, Modified_Date, Modified_IP_Address) VALUES('" & rs("Supplier_No") & "','" & rs("Supplier_Code") & "','" & rs("Supplier") & "','" & rs("Address_1") & "','" & rs("Address_2") & "','" & rs("Address_3") & "','" & rs("Address_4") & "','" & rs("Address_5") & "','" & rs("Phone_No") & "','" & rs("Fax_No") & "','" & rs("Credit_Limit") & "','" & rs("Account_Status_No") & "','" & rs("Active_Record") & "','" & rs("Entered_By") & "','" & rs("Entered_Date") & "','" & rs("Entered_IP_Address") & "','" & rs("Modified_By") & "','" & rs("Modified_Date") & "','" & rs("Modified_IP_Address") & "')"

rs.MoveNext

Loop

rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing

errbox:
If Err.Number > 0 Then

MsgBox Err.Description & " " & Err.Number
Exit Sub

End If
Sep 29 '16 #7
ADezii
8,834 Recognized Expert Expert
Another possible approach would be to create a parallel ADODB Recordset to populate the T_Suppliers Table:
Expand|Select|Wrap|Line Numbers
  1. '*******************************************************
  2. Dim rst2 As ADODB.Recordset
  3. Set rst2 = New ADODB.Recordset
  4. Dim intFldCtr As Integer
  5.  
  6. With rst2
  7.   .Source = "T_Suppliers"
  8.   .ActiveConnection = CurrentProject.Connection
  9.   .CursorType = adOpenDynamic
  10.   .LockType = adLockOptimistic
  11.     .Open
  12. End With
  13.  
  14. With rs
  15.   Do While Not .EOF
  16.     rst2.AddNew
  17.       For intFldCtr = 0 To .Fields.Count - 1
  18.         rst2.Fields(intFldCtr) = .Fields(intFldCtr)
  19.       Next
  20.     rst2.Update
  21.       .MoveNext
  22.   Loop
  23. End With
  24. '*******************************************************
Sep 29 '16 #8

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

Similar topics

3
9313
by: aaapaul | last post by:
Hallo ! I have a Table with a column "ordernumber" ordernumber A12 A45 A77 A88
1
1221
by: Skc | last post by:
I have a table with fields that contain values with inverted commas - i.e. "AAA" "BBB" etc... for field 1,2. How do I create a stored procedure that goes through the whole table and take out the...
7
2538
by: Samantha Penhale | last post by:
Hello, Thanks in advance for any insight you can offer. I've a ASP.NET project written in C#, two web forms, a lovely gob of using statements. I originally had one webform with all my fields and...
0
1090
by: Indira | last post by:
I am facing a problem. I have a long running Stored procedure. As per what I have read, if OleDbCommand.CommandTimeout is not set, as is in my case, the query should be timed out in 30 seconds...
3
2069
by: Eddie Suey | last post by:
I've created a console app that runs several SPROCs. While running the app if I access a CD or check my email, I get a timeout error. I'm not sure why. Is there a way to set a wait variable or...
7
3239
by: Jerry | last post by:
I'm trying to execute a stored procedure in a loop while paging through database table records but the stored procedure isn't running. I get the folowing error: The component 'adodb.connection'...
0
1620
by: eRTIS SQL | last post by:
hi, I want to use a stored procedure inside a stored procedure simulteanously changing the database. this is my base store procedure alter PROCEDURE create_file @dbname sysname AS declare...
1
7851
by: eRTIS SQL | last post by:
hi, I want to use a stored procedure inside a stored procedure simulteanously changing the database. this is my base store procedure alter PROCEDURE create_file @dbname sysname AS declare...
2
7432
parshupooja
by: parshupooja | last post by:
Hello , I am using asp.net C# SQL Server . I have form on asp.net page consists various fields. field 1, field 2 ------ upto field 10 on click of submit button i need to save all information...
10
4731
by: jmacfadyen | last post by:
I have been racking my brains to try to get this code working. I am trying to create an app to run a stored procedure on MS SQL server. The stored procedure is designed to delete a temoporary table...
0
7254
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
7153
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
7432
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...
0
7519
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...
0
5677
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,...
1
5079
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
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...
0
1585
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 ...
1
796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.