Put 2 text boxes (Text0 and Text1) on a form then copy the following code to
the module behind the form
Private Sub Text0_KeyPress(KeyAscii As Integer)
' This tests that the only printable characters entered are 0,1 or <space>
' It also tests that the allowable printable characters are in the correct
position
Select Case KeyAscii
Case 48, 49
If (Len(Me.Text0.Text) + 1) Mod 9 = 0 Then
KeyAscii = 0
End If
Case 32
' Test position
If (Len(Me.Text0.Text) + 1) Mod 9 <> 0 Then
KeyAscii = 0
End If
Case Is < 32
' do nothing
Case Else
'throw away
KeyAscii = 0
End Select
End Sub
Private Sub Text0_BeforeUpdate(Cancel As Integer)
' This checks that the total length of the code entered is correct
' i.e groups of eight digits terminated by a space.
Select Case Len(Me.Text0.Text) Mod 9
Case 0, 8
' OK
Case Else
Cancel = True
MsgBox "You must complete the code"
With Me.Text0
.SelStart = Len(Me.Text0.Text)
.SelLength = 0
End With
End Select
End Sub
Private Sub Text0_AfterUpdate()
' This converts the bit code to ASCII and
' enters it into Text1
Dim varInput As Variant
Dim intCount As Integer
Dim btCount As Byte
Dim btChar As Byte
Dim strOutput As String
varInput = Split(Me.Text0.Text, " ")
For intCount = LBound(varInput) To UBound(varInput)
btChar = 0
For btCount = 0 To 7
btChar = btChar Or _
Val(Mid(varInput(intCount), 8 - btCount, 1)) * 2 ^
btCount
Next
strOutput = strOutput & Chr(btChar)
Next
Me.Text1 = strOutput
End Sub
Private Sub Text1_AfterUpdate()
' This converts the ASCII to bit code and
' enters it into Text0
Dim varInput As Variant
Dim intCount As Integer
Dim btCount As Byte
Dim btChar As Byte
Dim strInput As String
Dim strOutput As String
strInput = Me.Text1.Text
For intCount = 1 To Len(strInput)
btChar = Asc(Mid(strInput, intCount, 1))
For btCount = 0 To 7
strOutput = strOutput _
& Abs(((btChar And (2 ^ (7 - btCount))) = (2 ^ (7 -
btCount))))
Next
strOutput = strOutput & " "
Next
If Len(strOutput) > 1 Then
strOutput = Left(strOutput, Len(strOutput) - 1)
End If
Me.Text0 = strOutput
End Sub
--
Terry Kreft
"Arnold" <ee*******@kc.rr.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
For some of my secondary students studying bits and bytes, I'd like to
make a form in which they can create secret codes with bytes. I'd like
the form to have a memo field that can only accept 0s and 1s and
spaces. I was envisioning the following:
--On after update or so, check the memo and only allow groups of eight
0s and 1s, and only single spaces.
--If students typed groups with < or > eight 0s or 1s, show a msgbox to
alert the them of their errors.
--With the click of a cmdbutton, show the students the result of their
byte coding either in a second memo field or in a report field.
My main problem is that I don't really know how to program ;-) Would
this whole coding of secret coding thing be possible?
I do have some cool code that I've come across that removes double,
triple, and all extra spaces from fields. It also inserts a period at
the end of the data in case the user forgot. Here's this code:
____________________
Private Sub MemoField_AfterUpdate()
Dim myLength As Long
Dim mydata As String
Dim lastchar As String
Dim X As Long
Dim temphold As String
If Not IsNull(MemoField) Then
mydata = Trim(MemoField)
myLength = Len(MemoField)
lastchar = Right(MemoField, 1)
End If
If lastchar = "." Or mydata = "" Then
Else
MemoField = mydata & "."
End If
temphold = ""
mydata = ""
For X = 1 To myLength
lastchar = Mid(MemoField, X, 1)
If temphold = " " And lastchar = " " Then
GoTo getnext
Else
temphold = lastchar
mydata = mydata & temphold
End If
getnext:
Next
MemoField = mydata
End Sub
____________________
Private Sub MemoField_LostFocus()
Dim myLength As Long
Dim mydata As String
Dim lastchar As String
If Not IsNull(MemoField) Then
mydata = Trim(MemoField)
myLength = Len(MemoField)
lastchar = Right(MemoField, 1)
End If
If lastchar = "." Or mydata = "" Then
Else
mydata = mydata & "."
MemoField = mydata
End If
End Sub
____________________
Many thanks in advance.