Visual Basic/SQL problem

Status
Not open for further replies.

maddmurph

Ars Tribunus Militum
1,986
Ok I am trying to write a program that will scan a cd and database all the files on the cd, I have that part working but when I try and write the sql code to get it to search the database, I am getting an error.
Here is the code

Private Sub cmdsubmit_Click()

Dim SqlStmt As String
Dim resultcount As Integer
Dim resultlistcount As Integer
Dim resultstring As String

If OptfindCD = True Then
SqlStmt = "SELECT [CD Name], [Path], [File Name] from [CD Explorer] where [CD Name] = " + txtsearch.Text
ElseIf optfindfile = True Then
SqlStmt = "SELECT [CD Name], [Path], [File Name] from [CD Explorer] where [File Name] = " + txtsearch.Text
End If


dbcdexplorerquery.RecordSource = SqlStmt
dbcdexplorerquery.Refresh

dbcdexplorerquery.Recordset.MoveFirst
resultlistcount = dbcdexplorerquery.Recordset.Fields(0)
resultcount = 0
ReDim ids(0 To resultlistcount - 1)

For resultcount = 0 To resultlistcount - 1
resultstring = dbcdexplorerquery.Recordset.Fields("[CD Name]") + dbcdexplorerquery.Recordset.Fields("Path") + dbcdexplorerquery.Recordset.Fields("[File Name]")
lstsearchresults.AddItem (resultstring)
ids(resultcount) = dbcdexplorerquery.Recordset.Fields("[CD Name]")
dbcdexplorerquery.Recordset.MoveNext
Next resultcount

End Sub

The error says "Run Time Error '3061':
Too Few Parameters. Expected 1.

When I hit debug it highlights the line of code that is bolded.
 

Kurt

Ars Tribunus Angusticlavius
8,820
Subscriptor++
You'll need to put single quotes around the search string:

If OptfindCD = True Then
SqlStmt = "SELECT [CD Name], [Path], [File Name] from [CD Explorer] where [CD Name] = '" + txtsearch.Text + "'"
ElseIf optfindfile = True Then
SqlStmt = "SELECT [CD Name], [Path], [File Name] from [CD Explorer] where [File Name] = '" + txtsearch.Text + "'"
End If
 

maddmurph

Ars Tribunus Militum
1,986
after reading another post here I feel I should meantion this is homework, though I am not looking for people to do the work for me I just need to know what is wrong, so any instruction you could give me would be nice. Hopefully, no one has a real big problem with that as I have done the coding but it is buggy, if you feel otherwise please feel free to ask the mods to lock the thread

spammydavis jr.- I don't get your post, could you explain that one to me
 
maddmurph, the .MoveFirst is raising an error because your recordset is empty. You should check the .EOF and/or .BOF properties of your recordset before you do anything with it. If .EOF = True right after creating the recordset, then your query returned no records.

As for Jackass Joe's comment, Query Analyzer is a SQL Server client tool that allows you to run queries against SQL Server databases. I don't know if you're using SQL Server as your backend, but the idea is to spit out the query string you're submitting and try running it somwhere outside your code. That usually points out syntax errors quite quickly.
 
rs.MoveFirst
Do While Not rs.EOF
action you want to do
rs.MoveNext
Loop

OK, here goes.

rs.movefirst (start going through recordset)
You need to check to make sure that your recordset isn't empty or
you'll get an error. There are different ways to check this. This
example uses the Do While Not method.


Do While Not rs.EOF (checks current record to see if it's the end.
If it isn't, proceed)


action you want to do (manipulate the returned record)

rs.Movenext (move to next record in your recordset)

Loop (do it all over again)
 
Status
Not open for further replies.