Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

another combo box problem...with VB coding

Status
Not open for further replies.

rotschreck

Programmer
Jan 26, 2000
54
CA
Problem 1<br>
<br>
Using a combobox for Mining_Division, I am trying to write code to make a select case function that will send a letter value to Division_Letter.<br>
<br>
Private Sub Mining_Division_AfterUpdate(Cancel As Integer)<br>
Select Case Mining_Division<br>
Case Kenora<br>
Division_Letter = K<br>
Case &quot;Larder Lake&quot;<br>
Division_Letter = &quot;L&quot;<br>
End Select<br>
End Sub<br>
<br>
I have attempted to use the quote marks. I have attempted to remove the quote marks.<br>
<br>
Suggestion?<br>
<br>
Problem 2<br>
<br>
Using Date_Entered, I am trying to send the year involved to Year.<br>
<br>
Private Sub Date_Received_AfterUpdate(Cancel As Integer)<br>
Year = Year(Date_Received)<br>
End Sub<br>
<br>
What is wrong here?<br>
<br>
Thanks <p> <br><a href=mailto: > </a><br><a href= Eclectic Page</a><br>
 
If the variable Mining_Division contains a string<br>
then your case statements must be strings<br>
So <br>
<br>
Select Case Mining_Division '&lt;&lt;&lt;&lt;&lt;&lt; put a stop line here<br>
Case &quot;Kenora&quot;<br>
Division_Letter = K<br>
Case &quot;Larder Lake&quot;<br>
Division_Letter = &quot;L&quot;<br>
End Select<br>
End Sub<br>
<br>
Now if you are not sure what Mining_Division is or contains at the time it runs.<br>
Then put your cursor on the line above and Press the F9 key.<br>
It will put a red line there.<br>
Then run your code and it will stop on that line.<br>
Press F8 key once so it move to the next line.<br>
Press ctrl-G to bring up a debug window.<br>
Type in ?Mining_Division and press the enter key<br>
It will print out exactly what's in there at that moment in time.<br>
<br>
OK<br>
<br>
<br>
<p> DougP<br><a href=mailto: dposton@universal1.com> dposton@universal1.com</a><br><a href= > </a><br>
 
Not sure if that's you're exact code, but have you tried prefixing with Me!, ie<br>
Select Case Me!Mining_division<br>
<br>
Also, is the bound column the one that returns, ie. &quot;Larder Lake&quot;, or is it the ID? For this to work, it needs to be the string description, ie the &quot;Larder Lake&quot; column, unless you use:<br>
Me!Mining_division.column(1) or whatever column that division is in (zero based)<br>
<br>
<br>
--Jim
 
your second problem<br>
Year is a Function in Access VBA so you cannot use it for a variable.<br>
So I would use Year1 <br>
<br>
Same thing as above you have to know what is in your variable.<br>
So is Date_Received a &quot;Date&quot; type or is it a String<br>
you could convert it to a date then get the Year like this<br>
<br>
Dim Year1 As Integer<br>
Year1 = Year(CDate(Date_Received))<br>
<br>
<p> DougP<br><a href=mailto: dposton@universal1.com> dposton@universal1.com</a><br><a href= > </a><br>
 
Doug,<br>
<br>
tried your suggestion. Still doesn't work. Maybe something to do with the (Cancel As Integer). Its a thought though I don't really know how it would affect it.<br>
<br>
Jim,<br>
<br>
Huh?<br>
<br>
I tried your Me! idea. It doesn't seem to work. I'm sure if I understood the .column(1) stuff I might be in a better position to figure this out.<br>
<br>
Thanks <p> <br><a href=mailto: > </a><br><a href= Eclectic Page</a><br>
 
R,<br>
In your select case, you seem to want to refer to the combo box value. Your reference to the combobox was not prefixed with either Forms!formname or Me!. This could cause a problem. <br>
<br>
Next, your example showed &quot;Larder Lake&quot; as one of the possible values. My question is: is the Bound column the one that contains values such as &quot;Larder Lake&quot; or is it, say, an ID field. Anyway, if you did Dougp's suggestion and put a breakpoint in the code, you should be able to see what that combobox is returning as a value. You can take it from there, but remember, you MUST put quotes around the values if they're characters.<br>
--Jim
 
Jim,<br>
The field Mining Division is text. I believe this is a string variable. I'm unsure why I would have to prefix with Forms!formname or Me! Since I'm in the current form. Maybe Table!tablename would work?<br>
<br>
Doug,<br>
I think my last post was unclear. I tried your suggestion with the year1 for the Year function. It doesn't seem to work. What should the (Cancel As _______) be? I'm unsure if it should be blank or As Integer or As Date.<br>
<br>
Thanks both for the help though. :) <p> <br><a href=mailto: > </a><br><a href= Eclectic Page</a><br>
 
rotschreck,<br>
When you say 'field Mining Division', if you mean Field, as in a field in the table, bound to a textbox on the form, then you should prefix it with Me!. However, you said it's a 'string variable', so if it's a variable, where are you setting it's value?--If you are setting the value from the combobox, there are 2 issues:<br>
1. Don't name the variable the same as the field or combobox.<br>
2. I forgot what 2 was.<br>
3. Oh, now I remember. make sure the combobox's bound field is the one you really want, ie, if column 1 is bound, and column 1 is the description, this may not be what you want. A typical way is to show the description in the box, but bind the key--to do this you need to have description in column 1 and key in column 2, and set bound column to 2. Let me know if this works...<br>
--Jim
 
See back in your original post<br>
Private Sub Date_Received_AfterUpdate(Cancel As Integer)<br>
Year = Year(Date_Received)<br>
End Sub<br>
<br>
You cannot use &quot;Year=&quot; because &quot;Year&quot; is a Command word.<br>
So I added a 1 to the end. <br>
Year1 or it could be MyYear or whatever<br>
MyYear = Year(Date_Received)<br>
It cannot be Year by itself.<br>
If Date_Received is a String then use the Cdate to convert it to a Valid date, then the command word &quot;Year&quot; will extract the &quot;Year&quot; out of it. <br>
<br>
Where did the (Cancel As Integer) thing come from.<br>
Leave it alone.<br>
<p> DougP<br><a href=mailto: dposton@universal1.com> dposton@universal1.com</a><br><a href= > </a><br>
 
Thanks Jim,<br>
That explains alot.<br>
<br>
Doug,<br>
I was thinking it might be the (Cancel As Integer) because I put it blank and it allowed another of my functions to work in the Date_Received_AfterUpdate. When I put Year, it all malfunctionned. I changed Year to Year1 as you suggested, but it didn't change anything. I understand that it will change everything, once I figure what is going wrong with my code. I've tried to change the (Cancel As Integer) to (), so that I could at least have one function that worked. Neither of them work now.<br>
<br>
Does this help? <p> <br><a href=mailto: > </a><br><a href= Eclectic Page</a><br>
 
When you say it doesn't change anything then I guess you are still getting an error?<br>
Do this<br>
Delete the whole function completly<br>
Then Compile the Code to see if you have any other errors<br>
Click the &quot;Debug&quot; menu then &quot;Compile&quot;<br>
If you have no errors open the form in design view.<br>
Double click on the combo box to bring up properties.<br>
then look for the &quot;After_Update&quot; event<br>
Click in the box and click &quot;Event Procedure&quot;<br>
Then click the &quot;3 dots&quot; button to bring up the VBA code window.<br>
Next put in the both of these lines<br>
Dim Year1 As Integer<br>
Year1 = Year(CDate(Date_Received))<br>
and see what you get.<br>
OK<br>
This is a simple as it gets if you are still having errors then something else is corrupted in your database.<br>
Or tell us what the EXACT sytax of the Error, Not &quot;Oh its Uh something like&quot;<br>
<br>
<br>
<p> DougP<br><a href=mailto: dposton@universal1.com> dposton@universal1.com</a><br><a href= > </a><br>
 
Also<br>
In your VBA code window put a ' &quot;Single quote&quot; in front of the 'On Error line so it will turn green and be a comment<br>
When you run your code it will stopon an erroe pop up a dialog box but it will have an additinal &quot;Debug&quot; Button click it an it will stop on the exact line causing the problem in your code. If you put your mouse over a variable and move it back kand forth slowly you should see a yellow box pop up with the value of that variable.<br>
<br>
Also you can Press Ctrl-G to bring up a Debug window and <br>
in the bottom half type a ? question mark and some variable that you want to examine<br>
like ?Year1 it will print out what Year1 is<br>
<br>
You can alos force the code to stop without an error<br>
By putting the mouse on a non DIM line and pressing the F9 key ti will put a line line ther and stop on it<br>
Then Press F8 to move one line at a time and in conjunction with the Debug window examine everything that going on<br>
you must pass a variable first using F8 before it will have a value.<br>
<br>
OK <p> DougP<br><a href=mailto: dposton@universal1.com> dposton@universal1.com</a><br><a href= > </a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top