newbie question concerning if-then-else 
Author Message
 newbie question concerning if-then-else

I have been racking my brains on how to get the syntax right for an
if-then-else statement

I have a combo box on a form where the user chooses a province or state. In
the next box they fill in the country.

I want the country box to automatically enter the proper country based on
the province or state chosen.

I assume and if statement was the best route to take...maybe I am wrong?

If not what is the syntax to make it work? I keep getting errors.

Thanks

Todd



Mon, 30 Jul 2001 03:00:00 GMT  
 newbie question concerning if-then-else
How about posting  a snippet of code and indicating where the error occurs?
Quote:

>I have been racking my brains on how to get the syntax right for an
>if-then-else statement

>I have a combo box on a form where the user chooses a province or state. In
>the next box they fill in the country.

>I want the country box to automatically enter the proper country based on
>the province or state chosen.

>I assume and if statement was the best route to take...maybe I am wrong?

>If not what is the syntax to make it work? I keep getting errors.

>Thanks

>Todd



Mon, 30 Jul 2001 03:00:00 GMT  
 newbie question concerning if-then-else
If you insist on using a hard-coded if then statement, the syntax would be:

'Assuming the State is stored in the first column of the combo box
If Me!cmbBox.column(0) = "CA" then
    Me!Country = "USA"
elseif Me!cmbBox.column(0) = "CT" then
    Me!Country = "USA"
'more elseif statements here
endif

However, what I would recommend doing is the following:
1.  Create a table with two fields.  "State", and "Country"
2.  Write a function to return the country based on the state.
3.  The function would look like this:  (Keep in mind, the following
function will work faster if set the rs variable to a query that only pulls
the State you are looking for.)

Public Function GetCountry(MyState as string) as string
'Write your own error code
   Dim rs as recordset
   Set rs = currentdb.openrecordset("<<table name here")
   'Check to make sure records exist
   If not rs.recordcount > 0 then
      rs.close
      'Put error code here
   endif

   rs.movefirst
   do until rs.eof
      if rs!State = MyState then
         GetCountry = rs!Country
         rs.close
         exit function
      endif
      rs.movenext
   Loop
   rs.close
End Function

4.  Now, in the AfterUpdate event of the State combo box, you will put the
following code

     Me!Country = GetCountry(Me!cmboBoxState.column(0).text)

--
Paul Brower

"I Love DSL!"

Quote:
>I have been racking my brains on how to get the syntax right for an
>if-then-else statement

>I have a combo box on a form where the user chooses a province or state. In
>the next box they fill in the country.

>I want the country box to automatically enter the proper country based on
>the province or state chosen.

>I assume and if statement was the best route to take...maybe I am wrong?

>If not what is the syntax to make it work? I keep getting errors.

>Thanks

>Todd



Mon, 30 Jul 2001 03:00:00 GMT  
 newbie question concerning if-then-else
In case you are still looking for a method to do this, you might try the
select case statement as in the following example:

Private Sub cmbstateprov_BeforeUpdate(Cancel As Integer)
Select Case cmbstateprov
    Case "Alberta","Quebec", "Ontario" 'a few Canadian Provinces
            me!txtcountry = "Canada"
    Case "Alaska","California","Florida" ' a couple of States
            me!txtcountry = "United States"
    Case else
            me!txtcountry = "Unknown"
End Select

End Sub

or something to that effect.
Hope this helps.


Quote:
>I have been racking my brains on how to get the syntax right for an
>if-then-else statement

>I have a combo box on a form where the user chooses a province or state. In
>the next box they fill in the country.

>I want the country box to automatically enter the proper country based on
>the province or state chosen.

>I assume and if statement was the best route to take...maybe I am wrong?

>If not what is the syntax to make it work? I keep getting errors.

>Thanks

>Todd



Mon, 06 Aug 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Newbie question concerning colors.. please help :(

2. Follow-up newbie question concerning writing OCX.

3. Newbie Question concerning Enterprise

4. Newbie - Concerning calculated fields....

5. Dozy question concerning arrays

6. DAO Question Concerning a Word Mail Merge from Access

7. ProjectResourceAdd concern/question

8. API Question Concerning Shell/Exec VB5.0

9. another question concerning data objects and combo boxes

10. another question concerning data objects and combo boxes

11. MTS; question concerning

12. MTS;questions concerning

 

 
Powered by phpBB® Forum Software