473,386 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

VB help with SQL variables and textbox

2
Hi all, I'm new here and new to VB and development in general. I'm trying to write a simple CSR app with a VB front-end connected to an access database. The problem I'm having is writing a SQL query that will read a textbox value in the WHERE statement.

Expand|Select|Wrap|Line Numbers
  1. SELECT     [Event Date], [Due Date], Priority, [Event Status], Category, Contact, [Event Description], [Event Solution]
  2. FROM    contactname
  3. WHERE (Contact = form2.namesearch.text)
I've also tried things such as, WHERE (Contact = '" & form2.namesearch.text & "')
This doesn't work either.

I'm fairly ignorant when it comes to coding and claim Google as my only resource so I'm sure that there is a simple answer. Thanks in advance for the help with this.
Jul 21 '07 #1
10 7030
Hi all, I'm new here and new to VB and development in general. I'm trying to write a simple CSR app with a VB front-end connected to an access database. The problem I'm having is writing a SQL query that will read a textbox value in the WHERE statement.

Expand|Select|Wrap|Line Numbers
  1. SELECT     [Event Date], [Due Date], Priority, [Event Status], Category, Contact, [Event Description], [Event Solution]
  2. FROM    contactname
  3. WHERE (Contact = form2.namesearch.text)
I've also tried things such as, WHERE (Contact = '" & form2.namesearch.text & "')
This doesn't work either.

I'm fairly ignorant when it comes to coding and claim Google as my only resource so I'm sure that there is a simple answer. Thanks in advance for the help with this.
I use VB6 and I recently had a similar problem. And I have found that quotes are very important. :) Variable or objects need to been with in single quotes in side of double quotes like " 'variable' " So, give this a try.

SELECT (what you want)
FROM (where you want)
WHERE 'form2.namesearch.text'

So it should look something like:

RS = "SELECT [whatever] FROM [WHEREVER] WHERE contact ='form2.namesearch.text' "

Let me know what you get.
Jul 21 '07 #2
Killer42
8,435 Expert 8TB
Bsayre, you are on the right track.

You see, VB and SQL are quite separate. Generally, you are simply building building a string which will be passed to an SQL interpreter. This interpreter is outside of the VB environment and knows nothing about your controls. So you need to take the value (not the name) from your textbox and place it in the string, to be passed to the SQL interpreter.

That's just a general overview, and of course circumstances can vary depending on what type of database connection you're using, what DB-aware controls and so on.

I don't know exactly how you're building or using this SQL query. But here's an example which will build the query in a string, that can then be passed to the database...

Expand|Select|Wrap|Line Numbers
  1. Dim strQuery As String
  2. strQuery = "SELECT [Event Date], [Due Date], Priority, [Event Status], Category, Contact, [Event Description], [Event Solution] FROM contactname WHERE (Contact = '" & Form2.namesearch.Text & "')"
  3.  
As you can see, this is pretty much what you said that you have already tried. So I think we need to see more details about exactly how this query is being used.
Jul 21 '07 #3
Killer42
8,435 Expert 8TB
... So it should look something like:

RS = "SELECT [whatever] FROM [WHEREVER] WHERE contact ='form2.namesearch.text' "
I really think you've got that wrong. This would simply search for records that had the actual text "form2.namesearch.text" stored in the [contact] field. It's pretty unlikely that this is the desired result.
Jul 21 '07 #4
Bsayre, you are on the right track.

You see, VB and SQL are quite separate. Generally, you are simply building building a string which will be passed to an SQL interpreter. This interpreter is outside of the VB environment and knows nothing about your controls. So you need to take the value (not the name) from your textbox and place it in the string, to be passed to the SQL interpreter.

That's just a general overview, and of course circumstances can vary depending on what type of database connection you're using, what DB-aware controls and so on.

I don't know exactly how you're building or using this SQL query. But here's an example which will build the query in a string, that can then be passed to the database...

Expand|Select|Wrap|Line Numbers
  1. Dim strQuery As String
  2. strQuery = "SELECT [Event Date], [Due Date], Priority, [Event Status], Category, Contact, [Event Description], [Event Solution] FROM contactname WHERE (Contact = '" & Form2.namesearch.Text & "')"
  3.  
As you can see, this is pretty much what you said that you have already tried. So I think we need to see more details about exactly how this query is being used.

What's the difference between my post and yours other than you added all of the tables.

my Select [whatever] equals your Select [Event Date], [Due Date], Priority, [Event Status], Category, Contact, [Event Description], [Event Solution]

and my FROM [wherever] equals your FROM contact name

and my WHERE contact = 'form2.namesearch.text'

Im assuming youre adding the &"" to handle nulls. And maybe I misread his post but I thought that's exact what he wanted to do is read the textbox value for the WHERE function. Anyways, I just gave the format not actual tables. But if you see something different let me know. Another big helper for me is using the Visual Data Manger and using the SQL Statement function or the query builder. You can test your queries on your DB to insure they are producing the records youre looking for.
Jul 22 '07 #5
Killer42
8,435 Expert 8TB
What's the difference between my post and yours other than you added all of the tables.
The tables aren't significant. The fundamental difference is that you included the name of the control property in the string, while I told VB to include the value of the property.

As an analogy, consider the difference between these two stetements...
Debug.Print "form2.namesearch.text"
Debug.Print form2.namesearch.text
Do you think they will produce the same result?


Another big helper for me is using the Visual Data Manger and using the SQL Statement function or the query builder. You can test your queries on your DB to insure they are producing the records youre looking for.
I'd have to agree. The details will vary depending on what version you're using, of course. I use VB6, and if I need SQL I generally fire up MS Access, use its query builder to generate the SQL, then copy it back to VB.
Jul 22 '07 #6
Bsayre
2
Thanks for the feedback. I may try building the WHERE statement in access and taking the code from there. Right now I'm just using a freeware copy of MS VB 2005 Express. It has a query builder which I've tried to use but it doesn't (that I've found) have anything to "build" the variables that I'm looking for.

I'm using the query to simply limit search results by contact name, and I'd eventually like to also filter by status (open, closed, pending). So the user will input their name and have access to view all of the help requests they've submitted.
Jul 23 '07 #7
Killer42
8,435 Expert 8TB
Sorry I couldn't help more. My comments basically relate to the general process of building a string in VB, which will then be processed by an SQL interpreter outside of VB. It's possible that the situation has changed significantly in VB2005 with its query builder.
Jul 23 '07 #8
The tables aren't significant. The fundamental difference is that you included the name of the control property in the string, while I told VB to include the value of the property.

As an analogy, consider the difference between these two stetements...
Debug.Print "form2.namesearch.text"
Debug.Print form2.namesearch.text
Do you think they will produce the same result?


I'd have to agree. The details will vary depending on what version you're using, of course. I use VB6, and if I need SQL I generally fire up MS Access, use its query builder to generate the SQL, then copy it back to VB.

I agree with you when your printing quotes " " to the screen however, I have found that quotes in the query are reversed ' " " ' depending on where they are used. The following are examples that I have been using in my own applications.

Expand|Select|Wrap|Line Numbers
  1. DATAtow.RecordSource = "select * from messages where user =' " & (Mid(from(Index).Caption, 1, 3)) & " ' order by sent desc"
  2.  
This line is parsing out the senders name from a lable array CAPTION property and using it in the WHERE. But since im in quotes I have to reverse the quotes to retrieve the value of objects.

Expand|Select|Wrap|Line Numbers
  1. db.rs = "select * from messages where messageid=' " & infoAR(4) & " ' "
  2.  
Same thing here. But to use your example it would be like printing a quote to the screen. You have to reverse the quotes to do it. Anyways, that's how I have been getting it done. :)
Jul 24 '07 #9
Killer42
8,435 Expert 8TB
You're right that I used double quotes (") because it was a Print statement. In SQL you generally use single quotes ('). I was merely pointing out that an SQL string which contained something like this...
WHERE contact = 'form2.namesearch.text'
...would not be searching for the contents of a textbox. It would be searching for records matching the name of the textbox. You would need to concatenate the contents of the textbox between the single quote characters.

I think we just need to concede that we're both saying the same thing, in slightly different ways.
Jul 24 '07 #10
You're right that I used double quotes (") because it was a Print statement. In SQL you generally use single quotes ('). I was merely pointing out that an SQL string which contained something like this...
WHERE contact = 'form2.namesearch.text'
...would not be searching for the contents of a textbox. It would be searching for records matching the name of the textbox. You would need to concatenate the contents of the textbox between the single quote characters.

I think we just need to concede that we're both saying the same thing, in slightly different ways.
haha. Conceded. But youre right, I flipped my quotes around in the first post. I used " ' instead of ' ". But I concede. You are the victor. :)
Jul 24 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Steve | last post by:
Can anyone recommend the best way to pass a string back to the calling class (windows form) from a dialog that was shown modally using ShowDialog?
3
by: Harry | last post by:
Hi, Can anyone help. I have a asp.net page (c#), with two panels, both of which have asp:textboxes on. When I press the first button, the second panel shows fine. When I press the button in...
6
by: hb | last post by:
Hi, I have a page bill.aspx and its code-behind bill.aspx.cs. On bill.aspx I have: === Select a month: <asp:dropdownlist runat="server" id="lstDate" autopostback="True" /> <br> <asp:table...
3
by: Aaron | last post by:
Why do my session values return to nothing on post back? I want to click a button and have the row, as in: dataset.table(0).rows(THIS ROW NUMBER IS WHAT I AM TALKING ABOUT), either increment or...
12
by: Joel Byrd | last post by:
I'm having a little problem with using type-ahead functionality for an auto-suggest box. Sometimes, when I start to type something and the type-ahead shows up, the AJAX will send a request query...
4
by: Rich | last post by:
Hello, I have 3 textboxes and 1 combobox on a form. On entering the control I want to select all the text. I can make an array of textboxes like this: Dim arrTxt As TextBox() = {txt1, txt2,...
1
by: veljkoz | last post by:
Hi all, I have some class that's managing the user interface - CMan, and I'm passing it the struct array with some txtboxes in it. CMan than changes txtboxes. This struct is initialized like...
6
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I have a gridview control with a template column that has a textbox and when the control is bound to the datasource the textbox is filled ok. I then change what is in the textbox in the gridview...
2
by: thisismykindabyte | last post by:
Hey-o! Can anyone tell me how to make this work? Long story short, I would like to use text from TextBox1.Text as the functions I would like to call. Then I would like TextBox1.Text to show...
8
by: adarshyam | last post by:
hi.. I am new to vb.net i am doing a program using dynamic textboxes which involves two pages .. where values of textboxes in one page must be transfered to to other sex of textboxes in another page,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.