excel vba - VBA SQL recordsets in forloops -


Currently using Excel for VBA and code for using some tutorials and practices And

My job is to compare a range and within each cell there is string / text within each cell and it is specified in a variable. Then use this variable in a SQL statement for a query in a small database and return the results.

I have chosen the option to do this with a loop that receives the text of the current cells, specifies it in the variable and then executes the query and it is in another cell Pops, the problem is that the code will be okay out of the loop and I can change the name of the variable and everything is hunky dori when I get an error inside the loop

"run-time error '3705': An operation of operation No matter when the object is open "

  sub-NO6_Postcode () 'CONNECT variable variable CD ADODB.Connection as ADODB.Recordset Dim CMD as ADODB.Command set cn = new Set ADODB.Connection RS = New ADODB.Recordset set CMD = new ADODB.Command 'range dim myRange as the next dimension in the form of the dim-sqlQry as the string dim myCange set range as myRange = range (" A1: A10 ") Set nextUsed = range (" F1: F10 ") 'cn. Provider =" MSDASQL ".ConnectionString =" connection to DSN via DSN = LocalhostTest ". MyRange ending with .sqlQry = myCell.Text set cmd.ActiveConnection = cn cmd.CommandText = Select" Where To Name Test "for each myCell & amp;;; SqlQry & amp; "'" CMD. Type the command = Set the ADCMD text RS Source = CMD RSSSS.S.A.A.F.A. ("F1"). CopyframeRockCarsetRSI has asked Marcoll and All   

to always loop for any kind of records inside this error when specifying this

thanks

Try it out:

  sub-NO6_Postcode () 'connection variable as ADODB . In the form of CNDADAdB. DCRDED CMD as ACADDB ADOB Comand Set CN = New ADDB Connection Set RS = New ADODB Set the limit in the form of RCset set CMD = new ADODB.Command slow n as the slow dim range as myRange limitations nextUsed limit as sqlQry string slow myCell as myRange = range ("A1 : A10 ") Set nextUsed = range (" F1: F10 ") 'Connection to the database via CNN via DSN. Provider = "MSDASQL". ConnectionString = "DSN = Localhost Test". Open with MySpace for MyRange = 1. Call. Square sqlQry = MyRange.Cells (n). Set Value cmd.ActiveConnection = Cndcd. CommodityText = "Select from Selection 'from where name' '& amp; SqlQry & amp;"; "Cmd.CommandType = adCmdText set rs.Source = cmd rs.Open nextUsed.Cells (N) .CopyFromRecordset Ru.Close Next MyCell End Sub   

However if your query returns only one result you really do not need CopyFromRecordset and if it is not, you do not want to overwrite the data. < / Div>

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -