code help for dynamic button function & single field report

momaw nadon

Registered User.
Local time
Yesterday, 16:31
Joined
Apr 19, 2013
Messages
10
I'm still learning Access 2010 and having issues getting my buttons to work the way I need. I'm working on a simple address database.

First issue is with my edit/save button. On form load, my fields are locked and my button will read "edit". After clicking, my fields are unlocked, my search features are locked, and my button reads "save" just how I need it to. The issue happens when I try and save the field edits, lock fields, return search features, and get button to read "edit" again. I know I need to add some code into what I already have, but I'm running into a wall as I have tried many options to get it to work. Here is the code for this button:

Code:
Private Function Lockdown() 'locks controls at load
    Dim tb As Control
    Dim cb As Control
    Dim subf As Control
 
    For Each tb In Me.Controls
        If TypeOf tb Is TextBox Then
            tb.Locked = True
            tb.BackColor = 13882323
        End If
    Next tb
 
    For Each cb In Me.Controls
        If TypeOf cb Is ComboBox Then
            cb.Locked = False
            cb.BackColor = 16777215
        End If
    Next cb
 
    For Each subf In Me.Controls
        If TypeOf subf Is SubForm Then
            subf.Locked = True
        End If
    Next subf
 
End Function
Private Function UnlockFields() 'unlocks controls
    Dim tb As Control
    Dim cb As Control
    Dim subf As Control
 
    For Each tb In Me.Controls
        If TypeOf tb Is TextBox Then
            tb.Locked = False
            tb.BackColor = 16777215
        End If
    Next tb
 
    For Each cb In Me.Controls
        If TypeOf cb Is ComboBox Then
            cb.Locked = True
            cb.BackColor = 13882323
        End If
    Next cb
 
    For Each subf In Me.Controls
        If TypeOf subf Is SubForm Then
            subf.Locked = False
        End If
    Next subf
 
End Function
Private Sub cmdsiteedit_Click()
    Call UnlockFields
 
    Me.siteid.Enabled = False
    Me.local.Enabled = False
 
    If cbositesearch.Enabled = True Then
    cmdsiteedit.Caption = "Save"
    Else
    cmdsiteedit.Caption = "Edit"
    End If
 
End Sub

Second issue is with my report button. I have not been able to get this to work once. I have done many searches on single record reports, and have found the same code every time. I added that code into my database, but can't seem to get it to work. In my database there are two address (shipping and work location) which I would like to print out together. I have the work location on the main form and the shipping on a subform. There are and upwards of 150 locations I will have in my database, so I really need to be able to single out one set for a job. Though I'm wondering if this is even possible. Here is the code I'm working with:

Code:
Private Sub cmdrptadd_Click()
    Dim strReportName As String
    Dim strCriteria As String
 
    strReportName = "rptSiteAdd"
    strCriteria = "siteid='" & Me!siteid & "'"
    DoCmd.OpenReport strReportName, acViewPreview, , strCriteria
 
End Sub

Any help or suggestions to solve my issues would be greatly appreciated and I thank you for any assistances.
MN
 
For your 'Private Sub cmdsiteedit_Click()' why do you need to test the
'If cbositesearch.Enabled = True'
Can you not just set it's caption according to whether you're editing or saving.
In your report is the siteid a number data type, if so
strCriteria = "siteid=" & Me!siteid

David
 
Thank you for help on the report button. It works as needed now.

Though I'm not sure I understand what you are asking on my edit/save button. Would you be able to post me an example? Thank you for your time and help.
MN
 
I'm still needing help on my edit button.

Though can someone tell me why there is a pop up on the report button? I get a pop up asking for site number, but it has no bearing on what number you put into it, as it will report on the one you have selected in the form. Is there a way to have the code omit that step and just open the report?

MN
 

Users who are viewing this thread

Back
Top Bottom