Change a string to be able to use as a text box to access the properties

Benwatson

Registered User.
Local time
Today, 00:58
Joined
Oct 11, 2016
Messages
26
<SOLVED>Change a string to be able to use as a text box to access the properties

Right i have been racking my brain on this im sure its something simple but im not getting it. on a form i have 14 text boxes named txtbay1 - 14. Now what i want the code to use the value in that Textbox to find a record and see if a specific value is True/false. Then change the color of the text box depending on value. the tricky thing is i want it to flow through all 14 text boxes doing this check.

this is what i have, i hope you can understand what im trying to do any help would be amazing

PHP:
Public Function bayinuse()
Dim bay As String
Dim num As Integer
Dim txtbay As String
Dim rs As Recordset
Dim lngred As Long
Dim lnggreen As Long
lngred = RGB(255, 0, 0)
lnggreen = RGB(0, 255, 0)
Set rs = CurrentDb.OpenRecordset("tblbays", dbOpenSnapshot, dbReadOnly)

Do While num < 14
    num = num + 1
    bay = "bay " + CStr(num)
    txtbay = "Forms![frmbaydisplay].[txtbay" + num + "]"
        Do While rs!bay <> bay
            rs.FindNext
            Loop
    If rs!inuse = True Then
        txtbay.BackColor = lngred
    ElseIf rs!inuse = False Then
        txtbay.BackColor = lnggreen
    End If
    rs.MoveFirst
Loop
End Function

thanks in advance
 
Last edited:
Ben, I'm sure you have a business reason for building this database. However, you have jumped into the database and your post describes HOW you think something should be done. But you haven't told readers WHAT the something is.

Your idea might be right on target, but we need to know about the something before offering focused comments.

Even a jpg of your tables and form.

Good luck with your project.
 
i have a form, on this form it has 14 textboxes all labelled as Bay 1 to Bay 14. When a user checks in Trailer which ever Bay the user checks the trailer into the text box will go red. do do this i have a table in the background called tblbays which has a in use column. the form does a on timer event to check to see if the bay is in use. there will be 2 end users for this one end user will check in/out the trailer when it comes to the site. the other end user is just for viewing so they have a visual of what is waiting on the drive and whats on a bay.

now i can get it to work i have a code but its messy and long i was trying to to shorten it

PHP:
Set rs = CurrentDb.OpenRecordset("tblbays", dbOpenSnapshot, dbReadOnly)
rs.FindFirst "[Bay] = 'Bay 1'"
    If rs!inuse = True Then
        Forms![frmbaydisplay].[txtbay1].BackColor = lngred
    ElseIf rs!inuse = False Then
        Forms![frmbaydisplay].[txtbay1].BackColor = lnggreen
    End If
rs.MoveFirst
rs.FindNext "[Bay] = 'Bay 2'"
    If rs!inuse = True Then
        Forms![frmbaydisplay].[txtbay2].BackColor = lngred
    ElseIf rs!inuse = False Then
        Forms![frmbaydisplay].[txtbay2].BackColor = lnggreen
    End If
rs.MoveFirst
rs.FindNext "[Bay] = 'Bay 3'"
    If rs!inuse = True Then
        Forms![frmbaydisplay].[txtbay3].BackColor = lngred
    ElseIf rs!inuse = False Then
        Forms![frmbaydisplay].[txtbay3].BackColor = lnggreen
    End If
rs.MoveFirst
rs.FindNext "[Bay] = 'Bay 4'"
    If rs!inuse = True Then
        Forms![frmbaydisplay].[txtbay4].BackColor = lngred
    ElseIf rs!inuse = False Then
        Forms![frmbaydisplay].[txtbay4].BackColor = lnggreen
    End If
rs.MoveFirst
rs.FindNext "[Bay] = 'Bay 5'"
    If rs!inuse = True Then
        Forms![frmbaydisplay].[txtbay5].BackColor = lngred
    ElseIf rs!inuse = False Then
        Forms![frmbaydisplay].[txtbay5].BackColor = lnggreen
    End If
rs.MoveFirst
rs.FindNext "[Bay] = 'Bay 6'"
    If rs!inuse = True Then
        Forms![frmbaydisplay].[txtbay6].BackColor = lngred
    ElseIf rs!inuse = False Then
        Forms![frmbaydisplay].[txtbay6].BackColor = lnggreen
    End If
rs.MoveFirst
rs.FindNext "[Bay] = 'Bay 7'"
    If rs!inuse = True Then
        Forms![frmbaydisplay].[txtbay7].BackColor = lngred
    ElseIf rs!inuse = False Then
        Forms![frmbaydisplay].[txtbay7].BackColor = lnggreen
    End If
rs.MoveFirst
rs.FindNext "[Bay] = 'Bay 9'"
    If rs!inuse = True Then
        Forms![frmbaydisplay].[txtbay9].BackColor = lngred
    ElseIf rs!inuse = False Then
        Forms![frmbaydisplay].[txtbay9].BackColor = lnggreen
    End If
rs.MoveFirst
rs.FindNext "[Bay] = 'Bay 10a'"
    If rs!inuse = True Then
        Forms![frmbaydisplay].[txtbay10a].BackColor = lngred
    ElseIf rs!inuse = False Then
        Forms![frmbaydisplay].[txtbay10a].BackColor = lnggreen
    End If
rs.MoveFirst
rs.FindNext "[Bay] = 'Bay 14'"
    If rs!inuse = True Then
        Forms![frmbaydisplay].[txtbay14].BackColor = lngred
    ElseIf rs!inuse = False Then
        Forms![frmbaydisplay].[txtbay14].BackColor = lnggreen
    End If
rs.MoveFirst
rs.FindNext "[Bay] = 'Bay 12'"
    If rs!inuse = True Then
        Forms![frmbaydisplay].[txtbay12].BackColor = lngred
    ElseIf rs!inuse = False Then
        Forms![frmbaydisplay].[txtbay12].BackColor = lnggreen
    End If
rs.MoveFirst
rs.FindNext "[Bay] = 'Bay 11'"
    If rs!inuse = True Then
        Forms![frmbaydisplay].[txtbay11].BackColor = lngred
    ElseIf rs!inuse = False Then
        Forms![frmbaydisplay].[txtbay11].BackColor = lnggreen
    End If
rs.MoveFirst
rs.FindNext "[Bay] = 'Truck 1'"
    If rs!inuse = True Then
        Forms![frmbaydisplay].[txttruck1].BackColor = lngred
    ElseIf rs!inuse = False Then
        Forms![frmbaydisplay].[txttruck1].BackColor = lnggreen
    End If
rs.MoveFirst
rs.FindNext "[Bay] = 'Truck 2'"
    If rs!inuse = True Then
        Forms![frmbaydisplay].[txttruck2].BackColor = lngred
    ElseIf rs!inuse = False Then
        Forms![frmbaydisplay].[txttruck2].BackColor = lnggreen
    End If
rs.MoveFirst
rs.FindNext "[Bay] = 'Truck 3'"
    If rs!inuse = True Then
        Forms![frmbaydisplay].[txttruck3].BackColor = lngred
    ElseIf rs!inuse = False Then
        Forms![frmbaydisplay].[txttruck3].BackColor = lnggreen
    End If
rs.MoveFirst
rs.FindNext "[Bay] = 'Truck 4'"
    If rs!inuse = True Then
        Forms![frmbaydisplay].[txttruck4].BackColor = lngred
    ElseIf rs!inuse = False Then
        Forms![frmbaydisplay].[txttruck4].BackColor = lnggreen
    End If
    rs.MoveFirst
rs.FindNext "[Bay] = 'Truck 5'"
    If rs!inuse = True Then
        Forms![frmbaydisplay].[txttruck5].BackColor = lngred
    ElseIf rs!inuse = False Then
        Forms![frmbaydisplay].[txttruck5].BackColor = lnggreen
    End If
    rs.MoveFirst
 
basically im wondering if there is a way to get the string value of txtbay = "Forms![frmbaydisplay].[txtbay2]" to work like

txtbay.Backcolor = lngred
not have to do this
Forms![frmbaydisplay].[txtbay2].Backcolor = lngred
for every text box
 
Use conditional formatting
 
As arnel has suggested, you should review conditional formatting. Check out youtube for free tutorials (ms access conditional formatting form).

You could post a copy of your database (zip format) for readers to review. You could get more focused suggestions and advice.

Good luck with your project.
 
its ok i have found the fix i didn't set the variables correctly for ref this is the fixed code

PHP:
Dim Bay As String
Dim num As Integer
Dim Frm As Form
Dim txt As String
Dim txtbay As TextBox
Dim lngred As Long
Dim lnggreen As Long
lngred = RGB(255, 0, 0)
lnggreen = RGB(0, 255, 0)
Set rs = CurrentDb.OpenRecordset("tblbays", dbOpenSnapshot, dbReadOnly)

Do While num < 12
    num = num + 1
    Bay = "bay " + CStr(num)
    txt = "txtbay" + CStr(num)
    Set Frm = Forms("frmbaydisplay")
    Set txtbay = Frm(txt)
    Do While rs!Bay <> Bay
            rs.MoveNext
            Loop
        If rs!inuse = True Then
            txtbay.BackColor = lnggreen
        ElseIf rs!inuse = False Then
            txtbay.BackColor = lnggreen
    End If
    rs.MoveFirst
Loop
End Function

thanks for your help anyways
 
@Ben,

Can you post a copy of the database? I'd like to see what you have and how it works.
I'm not following your table or form design.
Where do you Dim rs?
With 14 textboxes why are you checking only 1 thru 12?
Good luck with your project.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom