Solved

Shorten My Code

Posted on 2009-04-10
7
350 Views
Last Modified: 2013-11-25
How can i shorten the code below?
Is there any other to make this code shorter?

Private Sub Marker()

Dim i As Integer

For i = 0 To 12

        cmdTableIndex(i).BackColor = vbWhite

        cmdTableIndex(i).Caption = ""

        cmdTabBill(i).BackColor = vbWhite

Next

    Dim cn As ADODB.Connection

    Dim rs As ADODB.Recordset

    Dim sql As String

 

    Call OPEN_CON(cn, "MyTable", "Winpos")

    Set rs = New ADODB.Recordset

    sql = "SELECT * FROM The_Table WHERE Table_No in (101,102,103,104,105,106,107,108,109,110,111,112,114)"

    rs.LockType = adLockOptimistic

    rs.CursorType = adOpenKeyset

    rs.Open sql, cn

    With rs

        Do While Not .EOF

            Select Case Trim(.Fields("Table_No").Value)

                Case 100

                     cmdTableIndex(0).BackColor = vbRed

                     cmdTableIndex(0).Caption = "Accomodated" & vbNewLine & "100"

                     cmdTabBill(0).BackColor = vbRed

                     

                Case 101

                     cmdTableIndex(1).BackColor = vbRed

                     cmdTableIndex(1).Caption = "Accomodated" & vbNewLine & "101"

                     cmdTabBill(1).BackColor = vbRed

                Case 102

                     cmdTableIndex(2).BackColor = vbRed

                     cmdTableIndex(2).Caption = "Accomodated" & vbNewLine & "102"

                     cmdTabBill(2).BackColor = vbRed

                Case 103

                     cmdTableIndex(3).BackColor = vbRed

                     cmdTableIndex(3).Caption = "Accomodated" & vbNewLine & "103"

                     cmdTabBill(3).BackColor = vbRed

                Case 104

                     cmdTableIndex(4).BackColor = vbRed

                     cmdTableIndex(4).Caption = "Accomodated" & vbNewLine & "104"

                     cmdTabBill(4).BackColor = vbRed

                Case 105

                     cmdTableIndex(5).BackColor = vbRed

                     cmdTableIndex(5).Caption = "Accomodated" & vbNewLine & "105"

                     cmdTabBill(5).BackColor = vbRed

                Case 106

                     cmdTableIndex(6).BackColor = vbRed

                     cmdTableIndex(6).Caption = "Accomodated" & vbNewLine & "106"

                     cmdTabBill(6).BackColor = vbRed

                Case 107

                     cmdTableIndex(7).BackColor = vbRed

                     cmdTableIndex(7).Caption = "Accomodated" & vbNewLine & "107"

                     cmdTabBill(7).BackColor = vbRed

                Case 108

                     cmdTableIndex(8).BackColor = vbRed

                     cmdTableIndex(8).Caption = "Accomodated" & vbNewLine & "108"

                     cmdTabBill(8).BackColor = vbRed

                Case 109

                     cmdTableIndex(9).BackColor = vbRed

                     cmdTableIndex(9).Caption = "Accomodated" & vbNewLine & "109"

                     cmdTabBill(9).BackColor = vbRed

                Case 110

                     cmdTableIndex(10).BackColor = vbRed

                     cmdTableIndex(10).Caption = "Accomodated" & vbNewLine & "110"

                     cmdTabBill(10).BackColor = vbRed

                Case 111

                     cmdTableIndex(11).BackColor = vbYellow

                     cmdTableIndex(11).Caption = "Accomodated" & vbNewLine & "111"

                     cmdTabBill(11).BackColor = vbRed

            End Select

            Call .MoveNext

        Loop

        .Close

    End With

    Set rs = Nothing

    Set cn = Nothing

End Sub

Open in new window

0
Comment
Question by:Whing Dela Cruz
  • 3
  • 2
  • 2
7 Comments
 
LVL 48

Accepted Solution

by:
jpaulino earned 300 total points
ID: 24115423
Try this way:
Private Sub Marker()

Dim i As Integer

For i = 0 To 12

        cmdTableIndex(i).BackColor = vbWhite

        cmdTableIndex(i).Caption = ""

        cmdTabBill(i).BackColor = vbWhite

Next

    Dim cn As ADODB.Connection

    Dim rs As ADODB.Recordset

    Dim sql As String

 

    Call OPEN_CON(cn, "MyTable", "Winpos")

    Set rs = New ADODB.Recordset

    sql = "SELECT * FROM The_Table WHERE Table_No in (101,102,103,104,105,106,107,108,109,110,111,112,114)"

    rs.LockType = adLockOptimistic

    rs.CursorType = adOpenKeyset

    rs.Open sql, cn

    With rs

        Dim strTable As String

        Dim intTable As Integer

        

        Do While Not .EOF

        

            strTable = Right(Trim(.Fields("Table_No").Value), 2)

            intTable = CInt(strTable)

            

            If intTable >= 0 And intTable <= 11 Then

                     cmdTableIndex(intTable).BackColor = vbRed

                     cmdTableIndex(intTable).Caption = "Accomodated" & vbNewLine & "1" & strTable

                     cmdTabBill(intTable).BackColor = vbRed

           End If

           .MoveNext

        Loop

        .Close

    End With

    Set rs = Nothing

    Set cn = Nothing

End Sub

Open in new window

0
 

Author Comment

by:Whing Dela Cruz
ID: 24115514
Thanks Its working..,
What about the line 14? is there any other way to shorten that line? If yes then i will post another question for that.
Thanks!
0
 
LVL 69

Assisted Solution

by:Éric Moreau
Éric Moreau earned 200 total points
ID: 24115537
jpaulino, that is not that easy. there is no direct relation between the Table_no and the index on the form. for example, there is no item 113.

ERWIIN, an easy way to do it would be to give the Index property of your control the same value as your Table_No values (101 to 114). You don't have to keep the default 0 to x values. Only then, you would be able to use something like jpaulino showed:

        Do While Not .EOF

        

            inTable = cint(.Fields("Table_No").Value)

            

                     cmdTableIndex(intTable).BackColor = vbRed

                     cmdTableIndex(intTable).Caption = "Accomodated" & vbNewLine & intTable

                     cmdTabBill(intTable).BackColor = vbRed

           .MoveNext

        Loop

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Whing Dela Cruz
ID: 24115604
Sorry but you mean to say that i must change the  index as from 0 to 101 or 1 to 102 and so on..? and then proceed the given code?
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 24115705
yes
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24115725
emoreau for 113 you're right but I have assumed that he want's to skip it.
0
 

Author Comment

by:Whing Dela Cruz
ID: 24115733
Ah Thanks!
i will try it and maybe i post another question in relation with this topic.
thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visual Basic Excel Formatting error 4 85
using Access 8 59
Using "ScreenUpdating" 6 55
Convert VB6 MSXML2.ServerXMLHTTP process to C# 2 42
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now