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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Procedure Not Increasing Correctly 3

Status
Not open for further replies.

quest4

Technical User
Aug 27, 2001
735
US
Good afternoon. I have a OnKeyDown event procedure, which when CTRL-b is pressed It increase the value of PartGroup by one. It is not increasing it at all. Can someone please look at this code and tell me what I did wrong?
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim ControlDDown As Integer
ControlDDown = (KeyCode = vbKeyD And Shift And acCtrlMask) > 0

If ControlDDown = True Then
Me!PartGroup = Me!PartGroup + 1
Me!ItemNumber = Nz(DMax("[ItemNumber]", "stblECN_BOM", "[PartGroup] = " & Me!PartGroup), 0) + 1
End If
End Sub
I haven't gotten far enoughto know if it is resetting ItemNumber to to 1 yet. Thank you very much to anyone who helps straighten this out
 
Hi quest4,

You seem to have questions about this in several different places but the problem is that you are checking for <Ctrl><D> in your code so it does nothing on <Ctrl><B>.

Enjoy,
Tony
 
The reason that is is failing is that ControlDDown will always be equal to 0.

The value of Shift will be either 0 or 1, and the value of acCtrlMask is 2. Whenever you AND these together, you get a 0. You will also find that the logical AND operations are performed before the equality comparision operator. In other words, without parenthesis, you comparision is actually

(KeyCode = (vbKeyD And Shift And acCtrlMask))

with the expression (vbKeyD And Shift And acCtrlMask) being evaluated before comparing to KeyCode.

I would suggest that you try the following

If (Shift = 1) then
ControlDDown = (KeyCode = vbKey)
...
I'm not sure what role acCtrlMask is playing in your intended logic.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Thank you very one for your input. I tried changing the CTRL-D to B, but nothing changed. CajunCenturion, I think I understand what you are saying. As far as the acctrMask goes, I am not sure myself, it came from a previous post where I ask for some help in getting CRTL-b to run a procedure. This one did not work either, still no increase in PartGroup. Any other suggestions? Thank you all for your input.
 
By Chance.....

have you set the FORM'S KeyPreview property to True/Yes...

if not, there won't even be any KeyPress evaluation...

****************************
Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. (Albert Einstein)

Robert L. Johnson III
MCSA, CNA, Net+, A+
w: rljohnso@stewart.com
h: wildmage@tampabay.rr.com
 
Hi CajunCenturion,

Shift is a bit field and bit 0 on/off represents the <Shift> key - 0 and 1 as you say. Bit 1 on/off represents the <Ctrl> key (and bit 2, the <Alt> key). acCtrlMask is the 'official' constant to use (value 2 as you note) to check for the <Ctrl> key, so ..

Shift And acCtrlMask will have a value of 2 if the <Ctrl> key is pressed - Shift, itself, may have a value of 2, 3, 6 or 7 depending on whether <Shift> and/or <Alt> has also been pressed.

If &quot;D&quot; has also been pressed then KeyCode = vbKeyD will be True and so the whole thing will be greater than zero and ControlDDown will be set to True; if not it will be set to False.

Enjoy,
Tony

quest4

You replied while I was writing this - I'm not sure I follow what change you made, but have you tried stepping through your code to see what is happening?

Tony
 
I am unclear whether you're looking for a ^b, ^B, ^d, or ^D, as all have been referenced at one time or another.

To Capture ^b (CNTRL Lower case b) use the following
ControlDDown = ((KeyCode = vbKeyB) And (Shift = 2))

For ^B
ControlDDown = ((KeyCode = vbKeyB) And (Shift = 3))

for ^d
ControlDDown = ((KeyCode = vbKeyD) And (Shift = 2))

for ^D
ControlDDown = ((KeyCode = vbKeyD) And (Shift = 3))

Also, you should probably define ControlDDown as boolean, and make sure that the KeyPreview property of the Form is set to Yes.


Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Hi CajunCenturion ,

ControlDDown = (KeyCode = vbKeyB And Shift And acCtrlMask) > 0

was posted by boriska40 in thread702-659297 earlier.

If Ctrl+B or Ctrl+Shift+B are pressed simultaneously –1 or True is returned. It definitely does work and I have to admit put my suggestion in the above thread in the shade.

Hi again quest4,

What I think you need to do is explain exactly where you are using this procedure, in the Main or Sub form. Also whereever you are using it, I think you will have to move the code to to the Main or Sub form’s On KeyDown event and make sure the Main or Sub form’s Key Preview property is set to Yes.

Hi Tony,

Hope you’re keeping well.

Bill
 
You are indeed correct Tony, but as the code was orignally written
(KeyCode = vbKeyD And Shift And acCtrlMask)
KeyCode is not be compared to vbKeyD, it is being compared to the expression (vbKeyD And Shift And acCtrlMask), which with acCtrlMask having a value of 2, can only result in one of two values, either 0 or 2, neither of which will favorably compare to the KeyCode. The parenthesis are critical in this case so that KeyCode being compared to vbKeyD, separatedly and independantly of Shift being compared to acCtrlMask, and then ANDing these two boolean expressions expression for the assignment to ControlDDown.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Thanks again for all of the input. The d was fudge fingers. This is in a subform where we are incrementing a PartGroup and an ItemNumber:
1 1 Part a
1 2 Part v
1 3 Part d
2 1 Part b
2 2 Partc
And that is the end result, the top is working great but when I press CTRL-b now I am getting a Run-Time Error 3075
Syntax error (missing operator)in query expression '[PartGroup] ='. The debugger hi-lites the Me.ItemNumber line. Here is my current code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim ControlBDown As Integer
ControlBDown = (KeyCode = vbKeyB And Shift And acCtrlMask) > 0

If ControlBDown = True Then
Me!PartGroup = Me!PartGroup + 1
Me!ItemNumber = Nz(DMax(&quot;[ItemNumber]&quot;, &quot;stblECN_BOM&quot;, &quot;[PartGroup] = &quot; & Me!PartGroup), 0) + 1
End If
End Sub
I have tried everything I can think of and now I am shooting blanks for ideas on what is wrong. Any sugegestion please. Thank you again for all of the assistance from everyone I deeply appreciate it.
 
I stand corrected. The precendence of operations does permit that statement to work.

ControlDDown = (KeyCode = vbKeyD And Shift And acCtrlMask) > 0

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Hi CajunCenturion and Tony,

There is a bit of a problem sometimes when posting. Both of your last postings weren't visible to me when I made my last posting, if that makes sense.

Both of your last postings have shown me the importance of the Shift key and how to test for it. I am very grateful to the two of you. It took a little while for it to sink in.

Thanks and *'s for you both as I think you both expert and this should be of great use to other members.

Who says that you can't teach an old dog new tricks?

Cheers

Bill

 
If [PartGroup] is not a numeric field, you may need to enclose it in quotes.

Me!ItemNumber = Nz(DMax(&quot;[ItemNumber]&quot;, &quot;stblECN_BOM&quot;, &quot;[PartGroup] = '&quot; & Me!PartGroup) & &quot;'&quot;, 0) + 1

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Thanks billpower, no day goes by where I don't learn something from sombebody.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Thanks for the star, Bill,

I, too, learn something every day (apart from how to find a job, that is)

Thanks also for the congrats; it looks a bit different there.

When there are a lot of posts in quick succession it can get a bit confusing - I've just come back and caught up - but with Cajun's last post and no comeback it looks like it's all sorted now.

Enjoy,
Tony
 
Thank you akll again for the input. CajunCenturion I put in those changes I get pretty much the same result except the error message is now '[PartGroup]='1' instead of '[PartGroup] ='. PartGroup and ItemNumber are both number. Here is my modified code:
Private Sub Form_BeforeInsert(Cancel As Integer)

If IsNull(Me!PartGroup) Then
Me!PartGroup = Nz(DMax(&quot;[PartGroup]&quot;, &quot;stblECN_BOM&quot;), 1)
End If

If IsNull(Me!ItemNumber) Then
Me!ItemNumber = Nz(DMax(&quot;[ItemNumber]&quot;, &quot;stblECN_BOM&quot;, &quot;[PartGroup] = '&quot; & Me!PartGroup) & &quot;'&quot;, 0) + 1
End If

End Sub
I can not see anything wrong. Any suggestions? Thanks you again to everyone who helped. Why is it the last hurdle is always the highest?
 
Since it appears that [PartGroup] is numeric, then you should remove the quotes.

If IsNull(Me!ItemNumber) Then
Me!ItemNumber = Nz(DMax(&quot;[ItemNumber]&quot;, &quot;stblECN_BOM&quot;, &quot;[PartGroup]=&quot; & Me!PartGroup), 0) + 1
End If



Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Hi quest4,

Looks like I spoke too soon.

The first time you had the error it was probably caused by Me!PartGroup being Null, but you have addressed that in your later code (in a new event, I see) and the likely reason for the error now is that the trailing quote is outside the parentheses. You have ..

... &quot;[PartGroup] = '&quot; & Me!PartGroup) & &quot;'&quot;, 0) + 1

.. where it should be ..

... &quot;[PartGroup] = '&quot; & Me!PartGroup & &quot;'&quot;), 0) + 1

However, making that change will probably give you a data type mismatch.

Have you tried removing the quotes (as Cajun said)? And, if so, has it worked?

Enjoy,
Tony



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top