dakota97 Posted November 29, 2003 Posted November 29, 2003 (edited) Hi all, I'm having trouble retrieving a record from the DB and displaying the field in a textbox. I want to type a customers phone number in textbox1 and then search the DB for a matching record and display the information in tebox2 For some reason it won't display anything in the textbox2 when I click the button. Here's my code: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim MyConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= pos.mdb") MyConnection.Open() Dim MyCommand As New OleDbCommand("SELECT * FROM Customers WHERE Phone LIKE'" & TextBox1.Text & "'", MyConnection) Dim MyReader As OleDbDataReader = MyCommand.ExecuteReader() While MyReader.Read TextBox2.Text = MyReader("Name") End While End Sub Thanks in advance, Chris Edited November 29, 2003 by Robby Quote if(computer.speed == "slow") { hamster.feed(); } if(computer.speed == "really slow") { hamster.kill(); BuyNewHamster(); }
Shirtster Posted December 8, 2003 Posted December 8, 2003 dude, why are you using LIKE if you want an exact match ? SELECT * FROM Customers WHERE Phone = '" & TextBox1.Text & "'" will produce an exact match. Don't forget to test for no match or your code may crash. Quote
Administrators PlausiblyDamp Posted December 8, 2003 Administrators Posted December 8, 2003 If you step through the code does the DataReader contain any valid results? Also you may want to trim any whitespace from the textbox before passing it into the select statement. Dim MyCommand As New OleDbCommand("SELECT * FROM Customers WHERE Phone='" & TextBox1.Text.Trim() & "'", MyConnection) Just another thought - not used access in ages (no intention of using it either) but does access use a single quote ' or a double quote to delimit strings? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
dakota97 Posted December 8, 2003 Author Posted December 8, 2003 Shirtster I don't want an exact match....if the user doesn't include (say) the area code, or doesn't include the -'s I want it to be able to pick the record....I'll try the suggestions and see what happens....thanks to the both of you Quote if(computer.speed == "slow") { hamster.feed(); } if(computer.speed == "really slow") { hamster.kill(); BuyNewHamster(); }
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.