Wrox Programmer Forums
|
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 11th, 2003, 02:29 PM
Authorized User
 
Join Date: Jun 2003
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dynamic IF Statement

I have 6 text boxes on a form with a submit button. When the submit button is pressed the program searches through a string to find out if any of the text that was inputed within the text boxes exists.

The problem I have is that I don't want to create 32 if statements. Is there a way to I could create a dynamic if statement? I tried to but it bombs out when it trys to evaluate the if statement..

Please Help!!


 Dim sIfStatement As String
        sIfStatement = "InStr(strTEXT," + Me.txtKeyWord1.Text + ", CompareMethod.Text)"
        If Me.txtKeyWord2.Text.Trim <> "" Then
            sIfStatement = sIfStatement + " or InStr(strTEXT, " + "me.txtKeyword2.text" + ", CompareMethod.Text)"
        End If
        If Me.txtKeyWord3.Text.Trim <> "" Then
            sIfStatement = sIfStatement + " or InStr(strTEXT, " + "me.txtKeyword3.text" + ", CompareMethod.Text)"
        End If
        If Me.txtKeyWord4.Text.Trim <> "" Then
            sIfStatement = sIfStatement + " or InStr(strTEXT, " + "me.txtKeyword4.text" + ", CompareMethod.Text)"
        End If
        If Me.txtKeyWord5.Text.Trim <> "" Then
            sIfStatement = sIfStatement + " or InStr(strTEXT, " + "me.txtKeyword5.text" + ", CompareMethod.Text)"
        End If
        If Me.txtKeyWord6.Text.Trim <> "" Then
            sIfStatement = sIfStatement + " or InStr(strTEXT, " + "me.txtKeyword6.text" + ", CompareMethod.Text)"
        End If

        If sIfStatement Then

        Do something


    end if
 
Old June 11th, 2003, 03:04 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

i think you have something wrong in your thinking..

first of all... you can't execute a string that have something inside just to see the results of it...

you have to think that you really need to do a search in the string, but only if the textboxes aren't empty..

so in that case you have only 6 if..then statements..

anyway..

you can put the textboxes in a collection and loop trough each one, and do an if inside each loop.. seeking for the text...

Gonzalo Bianchi
 
Old June 11th, 2003, 03:11 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Perhaps a case statement would be better?

Hal Levy
Daddyshome, LLC
 
Old June 11th, 2003, 03:43 PM
Authorized User
 
Join Date: Jun 2003
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes I have 6 text boxes and each text box could have two states one state having data in it and one state with no data so you have 6 ^2 = 6*6 or 36 combinations all together. So to do this without building a dynamic if statement it would take me 36 if statements combinations.

I know what you are saying loop through the text boxes within a collection but this will create 6 loops through the data instead of 1 so it is not the effiecient. The string values that I am looking at are very large many pages of data.

A select case would also yeild 36 combinations
 
Old June 11th, 2003, 04:12 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

ok.. let's see this other way..

you have 6 textboxes..
every one has text on it (besides if the text is blank)
well..
you want to know if any of that 6 piece of text is in another text...

your original post has something like:
instr(...) or instr(...)....

well.. you are doing only 6 if in that case.. since you are loking for only 1 piece of string..

and you are doing the six..
if you loop trough each textbox, you will not look for empty strings, because you will not use thats textboxes...

maybe we are missing something you know and we dont....

and how do you know that you have only 36 ifs???

Gonzalo Bianchi
 
Old June 13th, 2003, 08:05 AM
Authorized User
 
Join Date: Jun 2003
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok you are right I was just not thinking right. Sometimes I get mind block and this was one of those times. I did use 6 if statements but I had to loop through the data 6 times. But it does not seem to take as long as I thought.

Thanks for your help.
 
Old June 13th, 2003, 08:44 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I don't see what it is you are doing, all of the InStr() functions are inclosed in parenthesis.

Also, I do think there is a better way to handle the data validation. I would first of all create an array of textboxes just to make it easy to loop through the boxes, as mentioned. That is a given. Then I would create function to validate the string and the key word search. Only cal the function if the textbox has an entry. You can test this in your For Next loop using the Trim() function.

You coding seems to be a bit unconventional. Could you explain more what you are doing with the sIfStatement String and why the function are enclosed in parenthesis?




Larry Asher
 
Old June 13th, 2003, 09:16 AM
Authorized User
 
Join Date: Jun 2003
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Larry

I know looking at my code looks unconventional. That is because I have been doing so much asp and asp.net programming so I am use to building sql statements and stuff like that and the sort of html type of format I was trying to do.

Yes I got it to work it was very simple I just had mind block for a while.

Thanks





Similar Threads
Thread Thread Starter Forum Replies Last Post
help writing dynamic form data to dynamic table ublend SQL Server ASP 1 June 1st, 2007 08:09 AM
help writing dynamic form data to dynamic table ublend Classic ASP Professional 1 June 1st, 2007 08:08 AM
Help w/ dynamic SQL statement ssmith001 Perl 1 February 23rd, 2007 10:13 AM
Dynamic Columns & Dynamic Grouping ??? nileshgambhava SQL Server 2005 0 December 12th, 2006 12:45 AM
build switch statement to make dynamic crmpicco Javascript How-To 0 October 27th, 2005 05:44 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.