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

If KeyAscii = 13 Then (NEXT TEXTBOX!) 1

Status
Not open for further replies.

ToshTrent

Technical User
Jul 27, 2003
209
Private Sub txtPubCode_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then MsgBox "Enter"
End Sub



Hi what in the code below would stop the section above working?

Private Sub Form_Load()
Dim DirectoryTest As String
Dim iResponse As Integer

'Set Contents and Properties of Fixed Information Fields
lblUserName.BackStyle = 0 'Clears background
lblRequestDate.BackStyle = 0 'Clears background

lblUserName = User 'Displays Windows User Name
lblRequestDate.Caption = Format(Date, "dd/mm/yyyy") 'Displays date in Format

cboPublishers.AddItem "No Publishers Present"
cboMagazines.AddItem "No Magazines Present"

DirCheck:
DirectoryTest = Dir(DatabaseLocation)
If DirectoryTest = "" Or DatabaseLocation = "" Then
iResponse = MsgBox("Unable to locate Database, would you like to specify one now?", vbQuestion + vbYesNo, "Database Not Found")
If iResponse = vbYes Then frmConfigure.Show vbModal
If iResponse = vbNo Then Exit Sub 'Stop loading the form to prevent errors
GoTo DirCheck 'Repeat the directory check
End If

'Set New Record Sets and Connection
Set DBcn = New ADODB.Connection
Set rs = New ADODB.Recordset

DBcn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= " & DatabaseLocation & ";"

'Defines what table and field to retrive the information from
rs.Open "SELECT * FROM tbl_Client_Details", DBcn, adOpenStatic, adLockReadOnly, adCmdText

'Clears the combo
cboPublishers.Clear

'Repeats until all records have been added
Do Until rs.EOF
cboPublishers.AddItem UCase(rs.Fields("Client_Name"))
cboPublishers.ItemData(cboPublishers.ListCount - 1) = rs.Fields("client_Auto_ID")
rs.MoveNext
Loop

rs.Close 'Closes the recordset
cmdSave.Enabled = True 'Enables the save button to allow saving to the database
End Sub

[red]
Thankyou[/red]
Matt
 
Tosh,

Not much, use a constant instead of the 13 key code.

Private Sub txtPubCode_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then MsgBox "Enter"
End Sub

and know that if there is a Default button on your form, this will be activated and overwrite this code.

Regards,



yoshiweb.gif


"I'm an idealist. I don't know where I'm going but I'm on the way." — Carl Sandburg, 20th-century American poet and writer
 
Well Yoshi, you're certainly on da ball this evening, many thanks. I didn't realise a default button would stop it, thanks again.

Tosh

[red]
Thankyou[/red]
Matt
 
Tosh,

Again, No Problem.

Happy Codin!

Regards,



yoshiweb.gif


"I'm an idealist. I don't know where I'm going but I'm on the way." — Carl Sandburg, 20th-century American poet and writer
 
Actually Yoshi, one question which I am eager to have answered.

Is there away to have all this code below applied to all the textboxes on the form?

[red]On Key Press Event[/red]

txtPubCode.SelStart = 0

If KeyAscii > 96 And KeyAscii < 123 Then KeyAscii = KeyAscii - 32

If KeyAscii <> Reverse_Key Then txtPubCode.SelLength = 1

If KeyAscii = vbKeyReturn Then MsgBox &quot;Enter&quot;

If Len(txtPubCode) = 4 And txtPubCode.SelStart = 4 Then txtMagCode.SetFocus

[red]
Thankyou[/red]
Matt
 
Tosh, Place it in a routine of it's own.

[tt]
Private Sub SetBoxes(Source as TextBox, Dest as TextBox, KeyAsc as Integer)
Source.SelStart = 0
If KeyAsc > 96 And KeyAsc < 123 Then KeyAsc = KeyAsc - 32
If KeyAsc <> Reverse_Key Then Source.SelLength = 1

If KeyAsc = vbKeyReturn Then MsgBox &quot;Enter&quot;

If Len(Source.Text) = 4 And Source.SelStart = 4 Then Dest.SetFocus
End Sub
[/tt]

Then simply call the routine with the Source Box and The Destination Box as Variables when required, like so..

[tt]
Private Sub txtPubCode_KeyPress(KeyAscii As Integer)
Call SetBoxes (txtPubCode, txtMagCode, KeyAscii)
End Sub
[/tt]

Hope this Helps,

Regards,

yoshiweb.gif


&quot;I'm an idealist. I don't know where I'm going but I'm on the way.&quot; — Carl Sandburg, 20th-century American poet and writer
 
Yo Yoshi,
Brilliant Stuff!

Yet I seem to be getting a small problem with it, the code is as follows

[blue]
Sub FormatTextBoxes(Source As TextBox, KeyCode As Integer)

If KeyCode > 96 And KeyCode < 123 Then KeyCode = KeyCode - 32 'Forces text to Ucase

If KeyCode = vbKeyBack Then
Source.SelLength = 1
Exit Sub
End If

If KeyCode <> &H8 Then Source.SelLength = 1 'Over type letter one by one

If Len(Source) = Source.MaxLength Then SendKeys &quot;{tab}&quot; 'If reaches max characters then return

If KeyCode = vbKeyReturn Then
SendKeys &quot;{tab}&quot;
KeyCode = 0
End If
End Sub[/blue]

Works fine first time around, but when you try to overtype in the textbox the MaxLength handle kicks in straight after each character and moves to the next textbox,

So I thought, Ooo I'll try this...
[blue]
If Len(Source) = Source.MaxLength [red]-1[/red] Then SendKeys &quot;{tab}&quot; 'If reaches max characters then return
[/blue]
This works great for the over typing, but when I reach the Maximum length the cursor does not move down to the next text box due to MaxLength -1.

Is there away I can have the cursor move to the next line when the Maximum Length is reached, when new and when overtyped?

[red]
Many Thanks[/red]
Tosh
 
Tosh, I don't think i understand your code.

[tt]
=========QUOTE==========
Sub FormatTextBoxes(Source As TextBox, KeyCode As Integer)

If KeyCode > 96 And KeyCode < 123 Then KeyCode = KeyCode - 32 'Forces text to Ucase

If KeyCode = vbKeyBack Then
Source.SelLength = 1
Exit Sub
End If

If KeyCode <> &H8 Then Source.SelLength = 1 'Over type letter one by one

If Len(Source) = Source.MaxLength Then SendKeys &quot;{tab}&quot; 'If reaches max characters then return

If KeyCode = vbKeyReturn Then
SendKeys &quot;{tab}&quot;
KeyCode = 0
End If
End Sub
=======END QUOTE========
[/tt]

First, vbKeyBack and &H8 are the SAME key. don't understand the use of this. If you wish to do one thing with the key if pressed and another when not, then use the Else command and maintain structure in your usage of constants.

For Instance
[tt]

Sub FormatTextBoxes(Source As TextBox, KeyCode As Integer)

If KeyCode > 96 And KeyCode < 123 Then KeyCode = KeyCode - 32 'Forces text to Ucase

If KeyCode = vbKeyBack Then
Source.SelLength = 1
KeyCode = 0 'RESET KEYCODE
Exit Sub
Else
Source.SelLength = 1 'Over type letter one by one
If Len(Source) = Source.MaxLength Then
SendKeys &quot;{tab}&quot; 'If reaches max characters then return
End If
If KeyCode = vbKeyReturn Then
SendKeys &quot;{tab}&quot;
KeyCode = 0
End If
End If
End Sub
[/tt]

Like this, the code is OK, except you missed one thing which I told you to put in (tut tut!).
Change this line..

[tt]
If Len(Source) = Source.MaxLength Then
[/tt]

to

[tt]
If Len(Source) = Source.MaxLength And Source.SelStart = Source.MaxLength Then
[/tt]

This code puts a check in to move to the next box only if the length of the contents of the box AND the cursor is at the END of the box.

Try Again.

Regards,




yoshiweb.gif


&quot;I'm an idealist. I don't know where I'm going but I'm on the way.&quot; — Carl Sandburg, 20th-century American poet and writer
 
Oops thanks,
another problem which was causing it, is...

The code calling the procedure was under Key_Press aposed to Key_Down, yet the KeyAscii from a key_down event is different to the key_press, any reason for this?

Thank you so much for your help!
 
On a keypress event, the single key on it's own (e.g. 'A') is used to provide the ASCII Code.
On a keydown event, you have the option of reading in a a key combination and therefore use the VB Constants for those keys. (For Instance CTRL+SHIFT+A)
Definitions are as follows.

keycode
A key code, such as vbKeyF1 (the F1 key) or vbKeyHome (the HOME key). To specify key codes, use the constants in the Visual Basic (VB) object library in the Object Browser.

keyascii
An integer that returns a standard numeric ANSI keycode. Keyascii is passed by reference; changing it sends a different character to the object. Changing keyascii to 0 cancels the keystroke so the object receives no character.

Regards,



yoshiweb.gif


&quot;I'm an idealist. I don't know where I'm going but I'm on the way.&quot; — Carl Sandburg, 20th-century American poet and writer
 
Your just pure brains!

[red]
Thankyou[/red]
Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top