Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
sfjtraps
Yak Posting Veteran
65 Posts |
Posted - 2014-02-06 : 12:28:12
|
I'm trying to use a VBA application to SELECT data from SQL SERVER2012 express, and save the result in a variable. I'm having trouble understanding the syntax for assigning the result to the variable called ProductName. I pasted my code below (see the long string of question marks for what I think is the problem area). Help would be greatly appreciated. thanks, sfjtraps'Connect to the database'Select batch entered data and display it'**************************************************************************************Private Sub SelectBatchData()Dim ConnectionString As StringDim cnt As ADODB.ConnectionDim tblName As StringOn Error GoTo HandlerDim rs As ADODB.RecordsetDim strSQL As StringDim ProductName As Variant'Set the batch data table nametblName = "Batch_Data" 'Table Name'Build connection stringConnectionString = "Provider=SQLOLEDB.1;User ID=sa;Password=barney;Initial Catalog=Batch;Data Source=D142JEFFERSS\SQLEXPRESS"'Connect to database and select dataSet cnt = New ADODB.Connectioncnt.Open (ConnectionString) strSQL = "SELECT Product_Name FROM " & tblName Set rs = New ADODB.Recordsetrs.Open strSQL, cntProductName = ??????????????????????????????????????'Close recordset and connectionrs.Closecnt.CloseConnectionString = "Nothing"Exit Sub 'Exit sub here or handler will display an unnecessary msgbox'Error handler for bad data entered on the batch start screen'Message screen is displayed with the bad data entry descriptionHandler: If Err.Description = "" Then MsgBox strerrormsg Else MsgBox Err.Description End IfEnd Sub |
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2014-02-06 : 18:55:32
|
It has been a while since I used ADO Recordsets but...ProductName = rs.Fields("Product_Name").Value???===============================================================================There are two kinds of light -- the glow that illuminates, and the glare that obscures. -James Thurber (1894-1961) |
|
|
|
|
|