In this tutorial I will show you how to create a multiple number LCD, like the ones you see on cash register. For this you will need to have done part 2. If you have then you can move straight on otherwise you will need to look at part 2.
In a new form but the same project. The first thing you will need to do is copy the frame containing your shapes then paste it put when you do a message box will come up. Click no to that as we don't want to create a control array. and you will have to paste three times so that you will have four numbers. Once you have these you will need to name them. You will need to name them "shpL(x)", "shpC(x)", "shpM(x)" and "shpL(x)" ("x" being a number) the capitalized letters mean Left, Center, Middle and right they also need to be in this order. You will see why later when we add the code. To save time you can copy your button and text box from before.
Also you will need to make the text box max length as 5.
The code for the button is like the code from part 2 however we will be adding a check string and a mid string. Firstly we will create a clear function for the numbers so all you need to do for this is add these codes.
CODE
Private Sub clearrightmodule()
'Procedure for the right module
Me.shpL1.FillColor = &H8000000B
Me.shpL1.BackColor = &H8000000B
Me.shpL2.FillColor = &H8000000B
Me.shpL2.BackColor = &H8000000B
Me.shpL3.FillColor = &H8000000B
Me.shpL3.BackColor = &H8000000B
Me.shpL4.FillColor = &H8000000B
Me.shpL4.BackColor = &H8000000B
Me.shpL5.FillColor = &H8000000B
Me.shpL5.BackColor = &H8000000B
Me.shpL6.FillColor = &H8000000B
Me.shpL6.BackColor = &H8000000B
Me.shpL7.FillColor = &H8000000B
Me.shpL7.BackColor = &H8000000B
End Sub
Private Sub clearleftmodule()
'Procedure for the left module
Me.shpL1.FillColor = &H8000000B
Me.shpL1.BackColor = &H8000000B
Me.shpL2.FillColor = &H8000000B
Me.shpL2.BackColor = &H8000000B
Me.shpL3.FillColor = &H8000000B
Me.shpL3.BackColor = &H8000000B
Me.shpL4.FillColor = &H8000000B
Me.shpL4.BackColor = &H8000000B
Me.shpL5.FillColor = &H8000000B
Me.shpL5.BackColor = &H8000000B
Me.shpL6.FillColor = &H8000000B
Me.shpL6.BackColor = &H8000000B
Me.shpL7.FillColor = &H8000000B
Me.shpL7.BackColor = &H8000000B
End Sub
Private Sub clearcentremodule()
'Procedure for the centre module
Me.shpC1.FillColor = &H8000000B
Me.shpC1.BackColor = &H8000000B
Me.shpC2.FillColor = &H8000000B
Me.shpC2.BackColor = &H8000000B
Me.shpC3.FillColor = &H8000000B
Me.shpC3.BackColor = &H8000000B
Me.shpC4.FillColor = &H8000000B
Me.shpC4.BackColor = &H8000000B
Me.shpC5.FillColor = &H8000000B
Me.shpC5.BackColor = &H8000000B
Me.shpC6.FillColor = &H8000000B
Me.shpC6.BackColor = &H8000000B
Me.shpC7.FillColor = &H8000000B
Me.shpC7.BackColor = &H8000000B
End Sub
Private Sub clearmiddlemodule()
'Procedure for the middle module
Me.shpM1.FillColor = &H8000000B
Me.shpM1.BackColor = &H8000000B
Me.shpM2.FillColor = &H8000000B
Me.shpM2.BackColor = &H8000000B
Me.shpM3.FillColor = &H8000000B
Me.shpM3.BackColor = &H8000000B
Me.shpM4.FillColor = &H8000000B
Me.shpM4.BackColor = &H8000000B
Me.shpM5.FillColor = &H8000000B
Me.shpM5.BackColor = &H8000000B
Me.shpM6.FillColor = &H8000000B
Me.shpM6.BackColor = &H8000000B
Me.shpM7.FillColor = &H8000000B
Me.shpM7.BackColor = &H8000000B
End Sub
What these do is clear the numbers of the display. Also if you put this code in form_LOAD then it will do this automatically.
Secondly we will add the check string. We use this to make sure that the number entered in the text box is the correct format which can be changed. Through out the codes I not to sure on how to change the format as I am also learning to do this particular string and when i have worked it out I will edit this and create a snippet.
CODE
cs = InStr(1, txtFuel, ".", vbBinaryCompare)
If cs = 0 Or cs > 3 Then
MsgBox "Enter value correctly such as 0.09 or 99.00", , "Decimal point incorrect"
Exit Sub
End If
X = Len(txtFuel)
If X < 5 Then
MsgBox "This may cause an error, all numbers should be 4 digits with a decimal point in such as 00.75"
Call clearrightmodule
Call clearleftmodule
Call clearcentremodule
Call clearmiddlemodule
Exit Sub
End If
As you can see from the first message box it formats the number to #.## or ##.## however if you want different format to those then don't add this piece of code in or comment it.
The next part is to add the mid-string. This will look for a number in the text box and look for the code which will correspond to that number.
CODE
FirstNumber = Mid(txtCheckString, 1, 1)
SecondNumber = Mid(txtCheckString, 2, 1)
Dot = Mid(txtCheckString, 3, 1)
ThirdNumber = Mid(txtCheckString, 4, 1)
FourthNumber = Mid(txtCheckString, 5, 1)
As you can see if you have done part 2 you would be able to remeber that we used "FirstNumber = Mid(txtCheckString, 1, 1)", now we have added more to it. this is a very simple string to do but can easily be hard. simply because most people includng my self have accidently done something wrong and get an error but think it was something else.
Explain...
The first part of it is the name i.e. "FirstNumber" you don't have to call it this. You could call it apples or something but "FirstNumber" is very good for this as it will be the first number. The next bit is just saying what it is "Mid()". Inside the brackets we have three attributes the name of the subject usually a text box, then it is the character then how many from that character.
Example...
FristNUmber = Mid(txtCheckString, 1, 1) and say in the text box it has "Hello World" then the code would return "H" however, if the last number changed to say five FirstNumber = Mid(txtCheckString, 1, 5) then it would return "Hello". If you change the first number to three and kept it at five FirstNumber = Mid(txtCheckString, 3, 5) it would return with
"llo W".
Now that you know how the strings work we can put in the code for the numbers. It is the same as part 2.
CODE
If firstnumber = 1 Then
shpL1.FillColor = &H80000000
shpL1.BackColor = &H80000000
shpL2.FillColor = vbBlue
shpL2.BackColor = vbBlue
shpL3.FillColor = vbBlue
shpL3.BackColor = vbBlue
shpL4.FillColor = &H80000000
shpL4.BackColor = &H80000000
shpL5.FillColor = &H80000000
shpL5.BackColor = &H80000000
shpL6.FillColor = &H80000000
shpL6.BackColor = &H80000000
shpL7.FillColor = &H80000000
shpL7.FillColor = &H80000000
ElseIf firstnumber = 2 Then
shpL1.FillColor = vbBlue
shpL1.BackColor = vbBlue
shpL2.FillColor = vbBlue
shpL2.BackColor = &H80000000
shpL3.FillColor = &H80000000
shpL3.BackColor = vbBlue
shpL4.FillColor = vbBlue
shpL4.BackColor = vbBlue
shpL5.FillColor = vbBlue
shpL5.BackColor = vbBlue
shpL6.FillColor = vbBlue
shpL6.BackColor = vbBlue
shpL7.FillColor = &H80000000
shpL7.BackColor = &H80000000
ElseIf firstnumber = 3 Then
shpL1.FillColor = vbBlue
shpL1.BackColor = vbBlue
shpL2.FillColor = vbBlue
shpL2.BackColor = vbBlue
shpL3.FillColor = vbBlue
shpL3.BackColor = vbBlue
shpL4.FillColor = vbBlue
shpL4.BackColor = vbBlue
shpL5.FillColor = &H80000000
shpL5.BackColor = &H80000000
shpL6.FillColor = vbBlue
shpL6.BackColor = vbBlue
shpL7.FillColor = &H80000000
shpL7.BackColor = &H80000000
ElseIf firstnumber = 4 Then
shpL1.FillColor = &H80000000
shpL1.BackColor = &H80000000
shpL2.FillColor = vbBlue
shpL2.BackColor = vbBlue
shpL3.FillColor = vbBlue
shpL3.BackColor = vbBlue
shpL4.FillColor = &H80000000
shpL4.BackColor = &H80000000
shpL5.FillColor = &H80000000
shpL5.BackColor = &H80000000
shpL6.FillColor = vbBlue
shpL6.BackColor = vbBlue
shpL7.FillColor = vbBlue
shpL7.BackColor = vbBlue
ElseIf firstnumber = 5 Then
shpL1.FillColor = vbBlue
shpL1.BackColor = vbBlue
shpL2.FillColor = &H80000000
shpL2.BackColor = &H80000000
shpL3.FillColor = vbBlue
shpL3.BackColor = vbBlue
shpL4.FillColor = vbBlue
shpL4.BackColor = vbBlue
shpL5.FillColor = &H80000000
shpL5.BackColor = &H80000000
shpL6.FillColor = vbBlue
shpL6.BackColor = vbBlue
shpL7.FillColor = vbBlue
shpL7.BackColor = vbBlue
ElseIf firstnumber = 6 Then
shpL1.FillColor = vbBlue
shpL1.BackColor = vbBlue
shpL2.FillColor = &H80000000
shpL2.BackColor = &H80000000
shpL3.FillColor = vbBlue
shpL3.BackColor = vbBlue
shpL4.FillColor = vbBlue
shpL4.BackColor = vbBlue
shpL5.FillColor = vbBlue
shpL5.BackColor = vbBlue
shpL6.FillColor = vbBlue
shpL6.BackColor = vbBlue
shpL7.FillColor = vbBlue
shpL7.BackColor = vbBlue
ElseIf firstnumber = 7 Then
shpL1.FillColor = vbBlue
shpL1.BackColor = vbBlue
shpL2.FillColor = vbBlue
shpL2.BackColor = vbBlue
shpL3.FillColor = vbBlue
shpL3.BackColor = vbBlue
shpL4.FillColor = &H80000000
shpL4.BackColor = &H80000000
shpL5.FillColor = &H80000000
shpL5.BackColor = &H80000000
shpL6.FillColor = &H80000000
shpL6.BackColor = &H80000000
shpL7.FillColor = &H80000000
shpL7.BackColor = &H80000000
ElseIf firstnumber = 8 Then
shpL1.FillColor = vbBlue
shpL1.BackColor = vbBlue
shpL2.FillColor = vbBlue
shpL2.BackColor = vbBlue
shpL3.FillColor = vbBlue
shpL3.BackColor = vbBlue
shpL4.FillColor = vbBlue
shpL4.BackColor = vbBlue
shpL5.FillColor = vbBlue
shpL5.BackColor = vbBlue
shpL6.FillColor = vbBlue
shpL6.BackColor = vbBlue
shpL7.FillColor = vbBlue
shpL7.BackColor = vbBlue
ElseIf firstnumber = 9 Then
shpL1.FillColor = vbBlue
shpL1.BackColor = vbBlue
shpL2.FillColor = vbBlue
shpL2.BackColor = vbBlue
shpL3.FillColor = vbBlue
shpL3.BackColor = vbBlue
shpL4.FillColor = &H80000000
shpL4.BackColor = &H80000000
shpL5.FillColor = &H80000000
shpL5.BackColor = &H80000000
shpL6.FillColor = vbBlue
shpL6.BackColor = vbBlue
shpL7.FillColor = vbBlue
shpL7.BackColor = vbBlue
ElseIf firstnumber = 0 Then
shpL1.FillColor = vbBlue
shpL1.BackColor = vbBlue
shpL2.FillColor = vbBlue
shpL2.BackColor = vbBlue
shpL3.FillColor = vbBlue
shpL3.BackColor = vbBlue
shpL4.FillColor = vbBlue
shpL4.BackColor = vbBlue
shpL6.FillColor = &H80000000
shpL6.BackColor = &H80000000
shpL5.FillColor = vbBlue
shpL5.BackColor = vbBlue
shpL7.FillColor = vbBlue
shpL7.BackColor = vbBlue
End If
However, this will only work for the one number as know that we have three other numbers and a decimal point we need to create the same code as the one above but changed to fit the other numbers. I will show you how to start it but if you want to open the text file with the full code then open the attachment (Code for Numbers) at the bottom.
Instead of:
CODE
If FirstNumber = 1 Then
You would need to add
CODE
If SecondNumber = 1 Then
If ThirdNumber = 1 Then
If FourthNumber = 1 Then
Also to rember to change the name of the shapes "shpC", "shpM" and "shpR".
You also need to add
CODE
If Dot = "." Then
End If
What this will do if the dot is a Decimal Point then it will do nothing.
Just before you test it in the form_LOAD you need to declare some variables
CODE
Dim iFirstNumber As Integer
Dim iSecondNumber As Integer
Dim iThirdNumber As Integer
Dim iFourthNumber As Integer
Now time to test it so hit F5.
If you have any problems with this then don't hesitate to comment or contact me as I am here to help. Also any comments would be good as then I know how I am doing. However, at the moment I am not putting the full code on here but I will do soon.
Keep an eye out for part four when we add a Timer to it and the numbers automatically change.