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
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.
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.
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: - Dim sSQL As String
-
...
-
Set rs = cmd.Execute
-
-
' Loop Through the SQL Records and Insert into the Local Table
-
While Not rs.EOF
-
sSQL = ""
-
sSQL = sSQL & "INSERT INTO T_Suppliers ("
-
sSQL = sSQL & " Field1"
-
sSQL = sSQL & ", Field2"
-
sSQL = sSQL & ", Field3"
-
sSQL = sSQL & ") VALUES ("
-
sSQL = sSQL & " '" & NZ(rs!Field1, "") & "' "
-
sSQL = sSQL & ", '" & NZ(rs!Field2, "") & "' "
-
sSQL = sSQL & ", '" & NZ(rs!Field3, "") & "' "
-
sSQL = sSQL & ")"
-
-
CurrentDB.Execute sSQL, dbFailOnError
-
rs.MoveNext
-
Wend
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
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.
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
ADezii 8,834
Recognized Expert Expert
Another possible approach would be to create a parallel ADODB Recordset to populate the T_Suppliers Table: - '*******************************************************
-
Dim rst2 As ADODB.Recordset
-
Set rst2 = New ADODB.Recordset
-
Dim intFldCtr As Integer
-
-
With rst2
-
.Source = "T_Suppliers"
-
.ActiveConnection = CurrentProject.Connection
-
.CursorType = adOpenDynamic
-
.LockType = adLockOptimistic
-
.Open
-
End With
-
-
With rs
-
Do While Not .EOF
-
rst2.AddNew
-
For intFldCtr = 0 To .Fields.Count - 1
-
rst2.Fields(intFldCtr) = .Fields(intFldCtr)
-
Next
-
rst2.Update
-
.MoveNext
-
Loop
-
End With
-
'*******************************************************
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: aaapaul |
last post by:
Hallo !
I have a Table with a column "ordernumber"
ordernumber
A12
A45
A77
A88
|
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...
|
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...
|
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...
|
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...
| |
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'...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |
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...
|
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: 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...
|
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: 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...
|
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...
| |
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |