Results 1 to 10 of 10
  1. #1
    trevor40's Avatar
    trevor40 is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Feb 2014
    Location
    Australia
    Posts
    407

    trying to compact code

    I have these lines of code, how can I join them together?
    Controls("toggle" & i).Caption = rs![hex code]


    Controls("text" & i).Caption = rs![hex code]

    I tried
    Controls("toggle" & i).Caption = Controls("text" & i).Caption = rs![hex code]

    but then Controls("toggle" & i).Caption control comes up with true, the other one still works ok.




    1000 ways to skin a cat, allways looking for another one...
    Use MDB format for sample post. If your issue is fixed, mark the thread solved.
    Click on the star below if this has helped.

  2. #2
    amrut is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Location
    India
    Posts
    616
    The above code is ok for few controls. For more, may be a little odd to rename both type controls using same abbreviation, but is certainly effective.
    Rename all such controls like ctlName1,ctlName2,ctlName3.
    Then you can easily loop through them -
    Code:
    For i=1 to 3
    Me("ctlName" & i).Caption=rs![hex code]
    Next i

  3. #3
    trevor40's Avatar
    trevor40 is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Feb 2014
    Location
    Australia
    Posts
    407
    heres the code I am using, I'm already doing a loop in there for multiple controls.

    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim strMsg As String
    Dim lngMove As Long
    Dim col_sel As Long
    Dim rr As Long, gg As Long, bb As Long
    Set db = CurrentDb
    Set rs = db.OpenRecordset("html hex codes", dbOpenDynaset)
    SetColors (0)
    Me.listcnt = 1
    For i = 1 To 36
    Controls("toggle" & i).Caption = rs![hex code]
    Controls("toggle" & i).ForeColor = Val("&H" & Mid(rs![hex code], 1, 2)) + Val("&H" & Mid(rs![hex code], 3, 2)) * CLng(256) + Val("&H" & Mid(rs![hex code], 5, 2)) * CLng(65536)
    Controls("text" & i).Caption = rs![hex code]
    Controls("text" & i).BackColor = Val("&H" & Mid(rs![hex code], 1, 2)) + Val("&H" & Mid(rs![hex code], 3, 2)) * CLng(256) + Val("&H" & Mid(rs![hex code], 5, 2)) * CLng(65536)
    rs.MoveNext
    Next i
    rs.Close

    1000 ways to skin a cat, allways looking for another one...
    Use MDB format for sample post. If your issue is fixed, mark the thread solved.
    Click on the star below if this has helped.

  4. #4
    amrut is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Location
    India
    Posts
    616
    I think that is the correct way to do it.

  5. #5
    robrich22's Avatar
    robrich22 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Feb 2013
    Location
    Louisville, KY
    Posts
    41
    You could try something like this

    Code:
    Dim ctrl As Control
    Dim txt As TextBox
    
    For Each ctrl in Form.Controls
       If TypeOf ctrl Is TextBox Then
          ctrl = txt
          txt.Text = rs.Fields("whatever")
       End If
    Next

  6. #6
    trevor40's Avatar
    trevor40 is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Feb 2014
    Location
    Australia
    Posts
    407
    I'm trying to put these 2 lines of code together, no real reason just trying to reduce code line count

    Controls("toggle" & i).Caption = rs![hex code]

    Controls("text" & i).Caption = rs![hex code]

    I tried - Controls("text" & i).Caption = Conrols("toggle" & i).Caption = rs![hex code]

    First part returns true, the other has correct value

    Any other way to do this?

    1000 ways to skin a cat, allways looking for another one...
    Use MDB format for sample post. If your issue is fixed, mark the thread solved.
    Click on the star below if this has helped.

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    This

    Controls("toggle" & i).Caption = Controls("text" & i).Caption = rs![hex code]

    Is saying "set the caption of control on left side of the first = sign to the value of the expression on the right side of the first equal sign". The expression on right side will evaluate to either True or False.

    Can do what you already have working or cycle through all controls as shown by robrich22.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    ipisors is offline Access Developer
    Windows XP Access 2007
    Join Date
    Sep 2013
    Posts
    119
    I agree with Amrut; you're already doing it the correct way ... Why do you feel there is a need to combine those two lines onto one line?
    You might be able to get away with an old trick like: one independent statement : the next independent statement, which is how multiple lines of code are [sometimes successfully] run in the Immediate window, but this really doesn't make any sense to try to do as a regular habit (if ever) in a module.

  9. #9
    trevor40's Avatar
    trevor40 is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Feb 2014
    Location
    Australia
    Posts
    407
    I have a lot of code like this, update many fields with the same data, (Colors,code,references.) i was only trying to remove some lines of code, not desperate to do it, just curious if it could be done.

    1000 ways to skin a cat, allways looking for another one...
    Use MDB format for sample post. If your issue is fixed, mark the thread solved.
    Click on the star below if this has helped.

  10. #10
    ipisors is offline Access Developer
    Windows XP Access 2007
    Join Date
    Sep 2013
    Posts
    119
    Then you may want to consider maintaining a table that stores information about the form name, control name, type of setting (color, label, rowsource, controlsource), and then write a procedure that sets all of it there.
    In this suggestion I'm not necessarily aiming for "less code", but even more importantly: scalability, sustainability.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 7
    Last Post: 11-22-2013, 07:32 PM
  2. Compact on Close
    By libraccess in forum Programming
    Replies: 2
    Last Post: 05-05-2013, 09:05 AM
  3. Compact & Repair, Will I Run Into Problems?
    By robsworld78 in forum Access
    Replies: 1
    Last Post: 01-10-2012, 05:11 PM
  4. Compact and repair on close
    By colotazzman in forum Access
    Replies: 2
    Last Post: 05-20-2010, 02:04 PM
  5. MS Access compact problem
    By chiru_ongole in forum Access
    Replies: 5
    Last Post: 08-01-2009, 06:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums