A Visual Basic / Forms question..

Status
Not open for further replies.

brianosburn

Ars Scholae Palatinae
1,490
Subscriptor
I am currently designing a form in Outlook 2000 that has several check boxes. (I can't post a screen shot from here at work since our company's firewall thinks @home is a chat gateway).

Here's the general gist of it.

I have it divided up in 4 sections -

1) Various information (Ticket #, outage start time, outage stop time, Estimated Time of Repair). These are basically text input boxes with Ticket # and outage start time being required field. I had no problem setting that up.

2) Systems Affected. 3 Check box choices - Need to have it check to make sure that at least one of the three boxes are checked.

3) Systems Impacted. 10 check box choices - again, at least one of the boxes must be check.

4) Three tab frame containing 3 Text boxes - Impact Statement, Problem Description, Resolution description. I made the Impact statement and Problem description mandatory.

As you can see, it's sections 2 and 3 that are holding me up.

This is my first real VB project - I kinda taught myself as I went along.

Brian
 

Malyk

Ars Tribunus Angusticlavius
8,558
Subscriptor
Well, the first way to do this off the top of my head is to use an if statement and a couple of control variables.

Dim c1 as boolean
Dim c2 as boolean

c1 = false
c2 = false

...
'for number 2 in your list
if (checkbox1.checked = true or checkbox2.checked = true or checkbox3.checked = true) then
c1 = true
else
c1 = false
end if

'for number 3 in your list
if (checkbox1.checked = true or checkbox2.checked = true or checkbox3.checked = true ...) then
c2 = true
else
c2= false
end if

...

Then, when you submit the for do:

if (c1 and c2) then
'SAVE THE INFO
elseif (c1 = false and c2 = false) then
'DISPLAY GENERAL ERROR
elseif (c1 = false) then
'DISPLAY ERROR FOR THIS CONDITION
else
'DISPLAY ERROR FOR THIS CONDITION
end if

That should work I think. Again, not sure if it is the best way, but it will get you going.

Does that make sense?

Mike
 

brianosburn

Ars Scholae Palatinae
1,490
Subscriptor
Here's what I have as the code at this point:

code:<HR>
Code:
dim c1 as boolean
dim c2 as boolean<P>c1=false
c2=false<P>Function Item_Send()<P>if (SAcheck1.value=1 or SACheck2.value=1 or SAcheck3.value=1) then
c1=true
else
c1=false
end if<P>if (Si:check1.value=1 or Si:check2.value=1 or Si:check3.value=1 or Si:check4.value=1 or 
	Si:check5.value=1 or Si:check6.value=1 or Si:check7.value=1 or Si:check8.value=1 or 
	Si:check9.value=1 or Si:check10.value=1 or Si:check11.value=1) then
c2 = true
else
c2 = false
if(c1=false and c2=false) then
msgbox("Please select a System(s) Impacted as well as System(s) Affected
elseif (c1=false) then
msgbox ("Please select a System(s) Affected")
elseif (c2=false) then
msgbox ("Please select a System(s) Impacted")
else
end if
End Function

I'm getting : Expected end of statement Line No:2 whenever I try to run this form.

Again I apologise, haven't even had a chance to buy a book on VB scripting.

Bah.

Brian
 

Aeirould

Ars Tribunus Angusticlavius
7,535
Subscriptor
Malyk:

if (checkbox1.checked = true or checkbox2.checked = true or checkbox3.checked = true) then
c1 = true
else
c1 = false
end if

I've always found this to be more efficient:

c1 = ((checkbox1.value + checkbox2.value + checkbox3.value) > 0)

The benefit is that if you go down the road and say "Ok, now we need at least two of these checked", you just need to change the 0 to a 1, and you get five lines of code reduced to 1.

Edit:

Brian, you've got no close quote or close parenthese on your first MsgBox call.
 

brianosburn

Ars Scholae Palatinae
1,490
Subscriptor
I've always found this to be more efficient:

c1 = ((checkbox1.value + checkbox2.value + checkbox3.value) > 0)

The benefit is that if you go down the road and say "Ok, now we need at least two of these checked", you just need to change the 0 to a 1, and you get five lines of code reduced to 1.

Wouldn't I still need the else statements? Otherwise, how would it know if its true?

I never thought I'd apreciate the programmers forum.. :D

I did close the quotes out on that message box though.

Brian
 

brianosburn

Ars Scholae Palatinae
1,490
Subscriptor
As I stated before, I am a complete novice to VB.

It's just one of those things..will post my update code after I get done updating it :D

Brian

EDIT: Would that code replace the whole 5 lines?

code:<HR>
Code:
if (SAcheck1.value=1 or SACheck2.value=1 or SAcheck3.value=1) then
c1=true
else
c1=false 
end if

be replaced with
code:<HR>
Code:
c1 = ((checkbox1.value + checkbox2.value + checkbox3.value) > 0)

And the same for the other block.

I'm a little confused about that bit.

What happens if no boxes are checked - does c1=0 > 0?

Or does it set c1=false since 0 isn't greater the 0?

Brian
 

Malyk

Ars Tribunus Angusticlavius
8,558
Subscriptor
To use that code you would either have to change the variable type of c1 and c2 to integers or force the result of adding the checkboxes together to be a boolean.

And yes that code would replace the whole 5 lines.

If you change c1 and c2 to integers then you have to change the if statements where you try to save the values to reflect that. i.e. "if c1 < 1 or c2 < 1 then ..."

Mike
 

brianosburn

Ars Scholae Palatinae
1,490
Subscriptor
The latest and greatest...

code:<HR>
Code:
dim c1 as integers
dim c2 as integers
 
c1=0
c2=0
 
Function Item_Send()
 
'Checking the values of the Systems Affected Check Boxes
 
C1=(SAcheck1.value + SACheck2.value + SAcheck3.value)
 
'Checking the values of the Systems Impacted Check Boxes
 
C2=(Si:check1.value + Si:check2.value + Si:check3.value +
    Si:check4.value + Si:check5.value + Si:check6.value + 
    Si:check7.value + Si:check8.value +	Si:check9.value + 
    Si:check10.value + Si:check11.value) 
 
'Check to see if all required sections are there
 
if(c1 = 0 and c2 = 0) then
msgbox("Please select a System(s) Impacted as well as System(s) Affected')
 
elseif(c1 = 0 and c2 > 0) then
msgbox('Please select a System(s) Affected')
 
elseif(c1 > 0 and c2 = 0) then
msgbox('Please select a System(s) Impacted')
 
end if
End Function

Edit: Getting the following error:

Expected End of Statement
Line No:2


What is causing that?

Brian
 

HitScan

Ars Tribunus Militum
2,523
Subscriptor++
I don't think your "dim c1 as integers" will jive. Try Integer, singular. VB should pop a list of choices up for you as soon as you hit the space bar after the "as". (It's called Intellesense, and it kicks much, much ass for a VB programmer)

That's an odd error for it to complain about, but it's the only thing I can see that would make it complain.

HitScan
 

Aeirould

Ars Tribunus Angusticlavius
7,535
Subscriptor
Um, guys, the boolean data type is right.

Watch:

c1 = ((checkbox1.value + checkbox2.value + checkbox3.value)>0)

Reduces to:

c1 = ((1 + 0 + 0) > 0)

Reduces to:

c1 = (1 > 0)

Reduces to:

c1 = True

The same would apply if c1 = (0 > 0), which would result in c1 = False. Thus obviating the need for if/then/else constructs that simply set a flag.

Another common use for the technique is in VBScript parsing a checkbox:

isItOn = (Request.Form("checkbox")="on")

returns True if the HTML form's box was checked.

It's often forgotten that you can do this in VB, but VB does allow boolean logic of this nature.
 

brianosburn

Ars Scholae Palatinae
1,490
Subscriptor
uggh..I'm getting a headache :D

here's the code I have in now - I took out the >0 part, and made it pure integer.

code:<HR>
Code:
dim c1 as integer
dim c2 as integer
 
c1=0
c2=0
 
Function Item_Send()
 
'Checking the values of the Systems Affected Check Boxes
 
C1=(SAcheck1.value + SACheck2.value + SAcheck3.value)
 
'Checking the values of the Systems Impacted Check Boxes
 
C2=(Si:check1.value + Si:check2.value + Si:check3.value + Si:check4.value + Si:check5.value + Si:check6.value + Si:check7.value + Si:check8.value +	Si:check9.value + Si:check10.value + Si:check11.value) 
 
'Check to see if all required sections are there
 
if(c1 = 0 and c2 = 0) then
msgbox("Please select a System(s) Impacted as well as System(s) Affected')
 
elseif(c1 = 0 and c2 > 0) then
msgbox('Please select a System(s) Affected')
 
elseif(c1 > 0 and c2 = 0) then
msgbox('Please select a System(s) Impacted')
 
end if
End Function


Wraith - I go to Outlook 2000*Tools*Forms*Design Form and open up my form

Then I go to:Form*view Code.

Am I cutting and pastin this stuff into the wrong section?

Brian
 

Malyk

Ars Tribunus Angusticlavius
8,558
Subscriptor
That makes this VBA or Visual Basic for Applications if I'm not mistaken. I think that gives you most of the basic functionality of VB without all the add-in features, but I'm not sure.

So I think the code you have is correct. The only thing missing is what you need to do once you've determined that the screen is filled out correctly.

Are you still having trouble? If so, where and how?

Mike
 

brianosburn

Ars Scholae Palatinae
1,490
Subscriptor
Removing the as interger part worked there.

Now it's telling it's expecting a ')' on line 10.

code:<HR>
Code:
Dim c1 
Dim c2 
  
Function Item_Send()
c1 = 0
c2 = 0
 
c1 = (SAcheck1.Value + SACheck2.Value + SAcheck3.Value)
 
c2=(Si:check1.value + Si:check2.value + Si:check3.value + Si:check4.value + Si:check5.value + Si:check6.value + Si:check7.value + Si:check8.value + Si:check9.value + Si:check10.value + Si:check11.value)
 
If (c1 = 0 And c2 = 0) Then
msgbox("Please select a System Impacted as well as System Affected")
 
ElseIf (c1 = 0 And c2 > 0) Then
msgbox("Please select a System Affected")
 
ElseIf (c1 > 0 And c2 = 0) Then
msgbox("Please select a System Impacted")
 
End If
End Function
 

brianosburn

Ars Scholae Palatinae
1,490
Subscriptor
I swear to god you guys help me fix one thing and 9 other things pop up.


Aeirould - you were right. I had used the the names - SI:Check1 in the names of the checkboxes.

I named my checkboxes as such on the form - SACheck1, SACheck2, SAcheck3 (Systems Affected Checkboxes 1-3). SICheck1-11. (Systems Impacted Checkbox 1-11).

It NOW gives me the following error:

Object Required: SACheck1
Line No:8


argghh..

Brian
 

mikebaz

Ars Centurion
342
Subscriptor
I'm not an Outlook programmer normally, more of a full VB programmer, so this may be off, but here goes....

Controls on Outlook forms are not referenced the same way as controls on a full VB form; you need to take into account the object model. As an example, I customized an item form and placed a checkbox named "si:check2" and a command button named the default "CommandButton1" on page 2 of a mail message item (named "P.2", again the default), then was able to access the value like this:

code:<HR>
Code:
sub commandbutton1_click
	Set objPage = Item.GetInspector.ModifiedFormPages("P.2")
	Set objSiCheck2 = objPage.Controls("si:check2")
	msgbox objControl.Value
	msgbox MyValue
end sub

Notice the need to go through the ModifiedFormPages collection to get to the unbound control.

The Outlook help (at least in 2002) was fairly useless for me looking at this, so I used Sue Mosher's Slipstick site for more info. Look under "Unbound control".

Using this, the code will look something like this:

code:<HR>
Code:
Dim c1
Dim c2
Dim objPage<P>
Function Item_Send()<P>
c1 = 0
c2 = 0<P>' Set the right page name here for P.2, don't know it from 
' information in posts so far
Set objPage = Item.GetInspector.ModifiedFormPages("P.2")<P>
c1 = (objPage.Controls("SAcheck1").Value + objPage.Controls("SACheck2.Value") +
objPage.Controls("SAcheck3").Value)<P>
c2=(objPage.Controls("Si:check1").value + objPage.Controls("Si:check2").value + objPage.Controls("Si:check3").value +
 objPage.Controls("Si:check4").value + objPage.Controls("Si:check5").value + objPage.Controls("Si:check6").value + 
objPage.Controls("Si:check7").value + objPage.Controls("Si:check8").value + objPage.Controls("Si:check9").value + 
objPage.Controls("Si:check10").value + objPage.Controls("Si:check11").value)<P>
If (c1 = 0 And c2 = 0) Then<P>msgbox "Please select a System Impacted as well as System Affected"<P>item_send = false<P>
ElseIf (c1 = 0 And c2 > 0) Then<P>msgbox "Please select a System Affected"<P>item_send = false<P>
ElseIf (c1 > 0 And c2 = 0) Then<P>msgbox "Please select a System Impacted"<P>item_send = false<P>
End If<P>
End Function

The item_send=false stuff is to tell Outlook to cancel the send; otherwise it will proceed with the send (I think).

I probably don't have it quite right, but it's closer, I think. Without having a form to play with it's hard to be sure :) Sorry on the formatting of the code, still learning how to get the OpenForum to agree with me on it.

EDIT: fix msgbox calls which worked but were not right as per Wraith's message

[This message was edited by mikebaz on November 26, 2001 at 15:28.]
 

Wraith

Ars Tribunus Militum
1,712
The colons bothered me as well. What i also noted was that you're using msgbox() without giving it something to return into. It's a function so you either use

dim MyInt as integer
MyInt = msgbox(" text here ", vbOkOnly, " Title here ")

or

msgbox " text here ", vbOkOnly, " Title here "

but it should raise an error if you use the brackets with no return variable.
 

mikebaz

Ars Centurion
342
Subscriptor
Looking at the code now, off the top of my head, there is no page 2, just one page.

Hmm, without seeing it I'm guessing, but when I bring up the form designer at least I have multiple pages available via tabs, and Outlook appears to use the tab name as the page name, so whatever shows up on the first tab, the page with your form controls, use that for the page name, and see if that does it.
 

brianosburn

Ars Scholae Palatinae
1,490
Subscriptor
Okay, here goes.

Latest code..

code:<HR>
Code:
Dim c1
Dim c2
Dim objPage
 
Function Item_Send()
 
c1 = 0
c2 = 0
' Set the right page name here for P.2, don't know it from 
' information in posts so far
Set objPage = Item.GetInspector.ModifiedFormPages("P.1")
 
c1 = (objPage.Controls("SAcheck1").Value + objPage.Controls("SACheck2").Value + objPage.Controls("SAcheck3").Value)
 
c2=(objPage.Controls("Sicheck1").value + objPage.Controls("Sicheck2").value + objPage.Controls("Sicheck3").value + 
objPage.Controls("Sicheck4").value + objPage.Controls("Sicheck5").value + objPage.Controls("Sicheck6").value + 
objPage.Controls("Sicheck7").value + objPage.Controls("Sicheck8").value + objPage.Controls("Sicheck9").value + 
objPage.Controls("Sicheck10").value + objPage.Controls("Sicheck11").value)
 
If (c1 = 0 And c2 = 0) Then
msgbox "Please select a System Impacted as well as System Affected"
item_send = false
 
ElseIf (c1 = 0 And c2 > 0) Then
msgbox "Please select a System Affected"
item_send = false
 
ElseIf (c1 > 0 And c2 = 0) Then
msgbox "Please select a System Impacted"
item_send = false
 
End If
 
End Function

Here is the error I am getting now -
Object required 'objPage'
Line No:13

I'm not exactly sure what this means. Does it mean that it's not finding what objPage is?

Brian
 

brianosburn

Ars Scholae Palatinae
1,490
Subscriptor
Okay, I got it working..It now checks whenever they press send, it checks for the correct fields being filled.

My last task, and once this is done, I'm done buggin ya'll, is to get it to do it more then once.

For example, someone forgets to fill in both of the check box sections. He pressess send and it get rejected. He checks the appropriate box in one of the sections, but doesn't fill in the other. It then sends it without checking.

I just need to have it check everytime he tries to press send.

Latest and greatest code:

code:<HR>
Code:
Dim c1
Dim c2
Dim objPage
 
Function Item_Send()
 
c1 = 0
c2 = 0
 
	Set objPage = Item.GetInspector.ModifiedFormPages("Message")
 
	c1 = (objPage.Controls("SAcheck1").Value + objPage.Controls("SACheck2").Value + objPage.Controls("SAcheck3").Value)
 
	c2=(objPage.Controls("Sicheck1").value + objPage.Controls
("Sicheck2").value + objPage.Controls("Sicheck3").value + 
objPage.Controls("Sicheck4").value + objPage.Controls
("Sicheck5").value + objPage.Controls("Sicheck6").value + 
objPage.Controls("Sicheck7").value + objPage.Controls
("Sicheck8").value + objPage.Controls("Sicheck9").value + 
objPage.Controls("Sicheck10").value + objPage.Controls
("Sicheck11").value)
 
	If (c1 = 0 And c2 = 0) Then
	msgbox "Please select a System Impacted as well as System Affected"
	item_send = false	
 
	ElseIf (c1 = 0 And c2 > 0) Then
	msgbox "Please select a System Affected"
	item_send = false
 
	ElseIf (c1 > 0 And c2 = 0) Then
	msgbox "Please select a System Impacted"
	item_send = false
 	
	Elseif (c1 > 0 and c2 > 0) then item_send = true
 
	End If
 
End Function
 

brianosburn

Ars Scholae Palatinae
1,490
Subscriptor
I fixed it and got it working..When a box is checked, teh value is -1, not 1. :eek:

It runs smooth..thanks for your help..

code:<HR>
Code:
Dim c1
Dim c2
Dim objPage
 
Function Item_Send()
 
c1 = 0
c2 = 0
 
Set objPage = Item.GetInspector.ModifiedFormPages("Message")
 
c1 = (objPage.Controls("SAcheck1").Value + objPage.Controls("SACheck2").Value + objPage.Controls("SAcheck3").Value)
  
c2=(objPage.Controls("Sicheck1").value + objPage.Controls("Sicheck2").value + objPage.Controls("Sicheck3").value + 
objPage.Controls("Sicheck4").value + objPage.Controls("Sicheck5").value + objPage.Controls("Sicheck6").value + 
objPage.Controls("Sicheck7").value + objPage.Controls("Sicheck8").value + objPage.Controls("Sicheck9").value + 
objPage.Controls("Sicheck10").value + objPage.Controls("Sicheck11").value)
 
	If (c1 = 0 And c2 = 0) Then
	msgbox "Please select a System Impacted as well as System Affected"
	item_send = false 
 
	ElseIf (c1 = 0 And c2 < 0) Then
	msgbox "Please select a System Affected"
	item_send = false
 
	ElseIf (c1 < 0 And c2 = 0) Then
	msgbox "Please select a System Impacted"
	item_send = false
 
	Else 
	item_send = true
 
End If
 
End Function
 
Status
Not open for further replies.