Visual Basic 6.0 ADODB command to variable - sql

Here's the code
Dim cmd As ADODB.Command
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
conn.CursorLocation = adUseClient
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Nirvana.mdb" & "; Persist Security Info=False;"
Set cmd = New ADODB.Command
cmd.CommandText = "Select [Last Name] From Accounts Where [First Name]=#FN"
Set cmd.ActiveConnection = conn
cmd.Parameters.Item("#FN").Value = txtFirstName.Text
cmd.Execute
Set cmd.ActiveConnection = Nothing
Set cmd = Nothing
conn.Close
How do i pass the result of this query to a variable in Visual Basic 6.0 ?

Results are stored in a Record Set, create one:
dim rs as ADODB.RecordSet
Then instead of cmd.execute use:
rs.open cmd
if not rs.eof then
''//got rows
msgbox "first row, first col=" & rs.collect(0)
...

Related

Lost of lengthy integers when inserting Excel VBA SQL to Access database by

I have a csv containing lengthy numbers (like 3-4 billions) which i would like to insert them into access database through ADOBD sql in excel. The code works through but all these numbers become empty when i open the Table in the Access database.
I have double checked in the Designed View in Access that the data type is "Double" in the access and I have no clue why the lengthy intergers will be lost. Can I ask for your guidance on this please? The code has been simplified but the key point is how i could do something on the INSERT INTO line to force it reading lengthy numbers into access database? Many thanks.
Dim cnn As ADODB.Connection
Dim cmd As ADODB.Command
Dim rst As ADODB.Recordset
Dim MyConn
Dim strSQL, strSQL2, strSWL3 As String
Dim InsertDate As Date
Dim FileDate As String
Set cnn = New ADODB.Connection
MyConn = ThisWorkbook.Path & Application.PathSeparator & TARGET_DB
Set cnn = New ADODB.Connection
With cnn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.Open MyConn
End With
Set cmd = New ADODB.Command
Set cmd.ActiveConnection = cnn
Set rst = New ADODB.Recordset
rst.Open "SELECT [Date] FROM [Text;DATABASE=C:\].[Source.csv];", cnn, adOpenDynamic, adLockOptimistic
strSQL = "INSERT INTO [Daily] "
strSQL = strSQL & "SELECT [Date] As WDate, Code, [#Sold] As QtySold"
strSQL = strSQL & " FROM [Text;DATABASE=C:\].[TargetDB.csv]"
cmd.CommandText = strSQL
cmd.Execute
Set cmd = Nothing
rst.Close
Set rst = Nothing
cnn.Close
Set cnn = Nothing

Write in a field's record in an Access' table

Issue :
I wrote this code which is supposed to write in a specific record of the WPRCPart field, but for some unknown reasons, nothing happens when I run it.
There are no errors though, it's just that this execution produces no result.
The data type of the WPRCPart field is Short Text.
The data type of the PartNo field is Number and its values come from a value List in Combo Box which can contains Multiple values.
Any ideas ?
Dim conn As New ADODB.Connection
Dim connStr As String
Dim cmd As ADODB.Command
Dim rs As ADODB.Recordset
connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & "mypath" & "NewVersion.accdb" & ";"
conn.ConnectionString = connStr
conn.Open
Set cmd = New ADODB.Command
With cmd
.ActiveConnection = conn
.CommandText = "UPDATE OverViewNew Set WPRCPart = 'Yes' WHERE PartNo = 9165267"
End With
Set rs = cmd.Execute
'rs.close

Select Statement for Excel Datasource

I am connected to Excel sheet, which is acting as database. I need to select some records with where condition but I am getting error:
No value given for one or more required parameters
by using below code:
Dim conn As Object
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Set conn = CreateObject("ADODB.Connection")
XLName = "C:\Users\X\Desktop\rawdata.xlsx"
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" &
XLName & "';Extended Properties='Excel 12.0;HDR=NO;IMEX=1';"
conn.Open connString
rs.Open ("SELECT * FROM [data$] where industry='Government'"), conn,
adOpenDynamic, adLockReadOnly
Sheet1.Range("A2").CopyFromRecordset rs
rs.Close
conn.Close
When you set HDR=NO the column titles from the excel table will be ignored and it will be used internal names. See older answer: c#, oledb connection string issue

VBA. Cant get data from .mdb file. Operation is not allowed when object is closed

Here is my problem: I need to get data from .mdb file that is on the network hard drive. I am using ADODB to connect to it, but when i try to return field value from RecordSet that i created it returns error:
Operation is not allowed when the object is closed
Here is my code:
Dim rs As New ADODB.Recordset
Dim cmd As New ADODB.Command
cmd.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbPath & ".mdb"
cmd.CommandText = SQLRequst
cmd.CommandType = adCmdText
cmd.CommandTimeout = 900
rs.CursorLocation = adUseClient
rs.Open cmd, , adOpenForwardOnly, adLockReadOnly
Debug.Print rs.Fields(0)
Set cmd.ActiveConnection = Nothing
Set cmd = Nothing
Set rs.ActiveConnection = Nothing
At the moment i tried many different types of connection. The main thing thats makes me confused is that it connects to .mdb file and also creates RecordSet but most values are set to "Operation is not allowed when object is closed"
I think the problem is with the way i am connecting and getting records sets because I use same method to connect to sql database and it works just fine.
Any help will be appreciated
*Edited : SQLRequest = "SELECT * FROM tblStack WHERE StackID=XXXXX"
Changed code to this:
Dim rs As New ADODB.Recordset
Dim conn As New ADODB.Connection
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbPath & ".mdb"
rs.Open SQLRequst, conn, adOpenForwardOnly, adLockReadOnly
Debug.Print rs.Fields(0)
Set cmd.ActiveConnection = Nothing
Set cmd = Nothing
Set rs.ActiveConnection = Nothing
Still debug doesn't print anything and in Locals window shows that Fields(0).Value Operation is not allowed when the object is closed
Problem was in SQLRequst string :
SQLRequest = "SELECT * FROM tblStack WHERE StackID='XXXXX'"
It needed ''.

How to call stored procedure in SQL Server from Excel (Windows Authentication)?

What is the syntax to call stored procedure in SQL Server from Excel (VBA) using windows authentication?
'Remember to reference Microsoft ActiveX Data Objects via Tools - References
Dim strConn As String
Dim conn As New Connection
Dim cmd As New ADODB.Command
Dim rs As New Recordset
strConn = "DRIVER=SQL Server;SERVER=ServerName;DATABASE=DBname"
conn.ConnectionString = strConn
conn.Open
cmd.ActiveConnection = conn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "SPName"
cmd.Parameters.Refresh 'requires a trip to server / can add parameters manually using cmd.Parameters.Append
cmd.Parameters("#Param1").Value = ""
Set rs = cmd.Execute
If Not rs.EOF Then
'your code here
End If
conn.Close
Set rs = Nothing