Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Visual Basic (http://www.programmingforums.org/forum18.html)
-   -   Creating a Search function (http://www.programmingforums.org/showthread.php?t=12609)

Abyss Feb 18th, 2007 10:55 AM

Creating a Search function
 
Hey folks,

I'm new to VB but my job requires it from time to time so I'm picking it up in bits and pieces. At the minute, I'm working on a database in MSAccess that is storing a large number of records in no particular order. I am trying to add a search function in a seperate form, which will retrieve certain records based on 9 fields which are entered by the user. The primary key is pretty complex hence the 9 fields.

Can you recommend what is the best way to go about creating something like this?

We have a few books on VB and MSAccess in the office but unfortunately no Internet access for security reasons. If I even knew where to start looking in the books that would be some progress...

Thanks for the help!

Abyss Feb 23rd, 2007 4:24 PM

Got to work on this a bit more during the week. I can create the search successfully using input boxes. When I try to do it using text fields I get the error: "You entered an expression that has an invalid reference to the property bookmark"

Can anyone spot an error with this code?

:


Private Sub cmdSearch_Click()


Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sSQL As String

Set cn = CurrentProject.Connection
 
sSQL = "select * from CorrespRec where "
    sSQL = sSQL & " Month = " & Month.Value
    sSQL = sSQL & " and Year = " & Year.Value
    sSQL = sSQL & " and Centre = " & Centre.Value
    sSQL = sSQL & " and [Session] = " & Session.Value
    sSQL = sSQL & " and DS = " & DS.Value
    sSQL = sSQL & " and Bar = " & Bar.Value
    sSQL = sSQL & " and DOB = #" & Mid(DOB, 3, 2) & "/" & Left(DOB, 2) & "/" &
Right(DOB, 4) & "#"
    sSQL = sSQL & " and CHI = " & CHI.Value
    sSQL = sSQL & " and DateComm = #" & Mid(DateComm, 3, 2) & "/" &
Left(DateComm, 2) & "/" & Right(DateComm, 4) & "#"
    Set rs = cn.Execute(sSQL)
         
     
    DoCmd.OpenForm "CorrespRecTabbed"
    [Forms]![CorrespRecTabbed].RecordSource = strSQL
   
    DoCmd.Close acForm, "CorrespRecSearch1"

   
End Sub


I'd appreciate the help!

Arevos Feb 23rd, 2007 4:58 PM

Quote:

Originally Posted by Abyss (Post 124323)
Got to work on this a bit more during the week. I can create the search successfully using input boxes. When I try to do it using text fields I get the error: "You entered an expression that has an invalid reference to the property bookmark"

The TextBox class has the property Text, rather than Value, to denote the input.

As an aside, the way you've built up your SQL string would give a malicious user direct access to the database via an SQL injection attack. If this is just a local database that the user has access to anyway, then there probably isn't any problem. However, it's something of a bad habit to get into.

Abyss Feb 23rd, 2007 8:21 PM

Quote:

Originally Posted by Arevos (Post 124326)
The TextBox class has the property Text, rather than Value, to denote the input.

As an aside, the way you've built up your SQL string would give a malicious user direct access to the database via an SQL injection attack. If this is just a local database that the user has access to anyway, then there probably isn't any problem. However, it's something of a bad habit to get into.

Just out of interest, what would be the correct way to build the SQL string? This database is under minimal risk to be targetted by a malicious user though it would be good to know for the future.

Thanks for the textbox information also, I'll change it on Monday.

Arevos Feb 24th, 2007 8:16 AM

Quote:

Originally Posted by Abyss (Post 124332)
Just out of interest, what would be the correct way to build the SQL string? This database is under minimal risk to be targetted by a malicious user though it would be good to know for the future.

I haven't worked in VB.NET much, or really at all, but I suspect it would be something like this:
:

  1. Dim command As ADODB.Command
  2. Dim usernameParam As ADODB.Parameter
  3.  
  4. Set command = New ADODB.Command
  5.  
  6. command.CommandText = "SELECT * FROM users WHERE username = @username"
  7. command.ActiveConnection = CurrentProject.Connection
  8.  
  9. Set usernameParam = New ADODB.Parameter
  10.  
  11. usernameParam.ParameterName = "username"
  12. usernameParam.Type = adVarChar
  13. usernameParam.Value = "Bob"
  14.  
  15. command.Parameters.Append usernameParam
  16.  
  17. Set rs = command.Execute()

Long winded, I know. Maybe there's a more concise way.


All times are GMT -5. The time now is 1:58 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC