PDA

View Full Version : Solved: Visible vs. invisible



ailyn
10-28-2005, 05:47 AM
I have a form Products and I need it to show a couple of controls (buttons actually) only when in editing mode.
The form opens always in View only unless you click the button 'Edit_Products'. This is my code:

Private Sub Edit_Products_Click()
On Error GoTo Err_Edit_Products_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "Products"
stLinkCriteria = "[ProductId] =" & Me![ProductId]
DoCmd.Close
DoCmd.OpenForm stDocName, , , stLinkCriteria, acFormEdit
MkProduct.Visible = True
SvProduct.Visible = True
DltProduct.Visible = True

Exit_Edit_Products_Click:
Exit Sub

Err_Edit_Products_Click:
MsgBox Err.Description
Resume Exit_Edit_Products_Click

End Sub


When I click it, it says that the expression refers to an object that is closed or that doesn't exist. I checked the names over and over and they are ok. ?.?
Help, pls http://vbaexpress.com/forum/images/smilies/pray2.gif

chocobochick
10-28-2005, 06:18 AM
I was able to duplicate this error and found your problem. When you close the form before reopening it, the references to the form and its controls are removed from memory and you can no longer access them implicitly.

Instead, you can reference these controls explicitly by adding the form's identifier, like so:

Forms(stDocName).MkProduct.Visible = True
Forms(stDocName).SvProduct.Visible = True
Forms(stDocName).DltProduct.Visible = True


Alternatively, you could also declare a Form variable and reference commands this way:

Dim f as Form
DoCmd.OpenForm stDocName, , , , acFormEdit
Set f = Forms(stDocName)
f.MkProduct.Visible = True
f.SvProduct.Visible = True
f.DltProduct.Visible = True

ailyn
10-28-2005, 06:38 AM
It works!

http://vbaexpress.com/forum/images/smilies/notworthy.gif Thanks a lot!

ailyn
10-28-2005, 06:50 AM
btw, remember the one you helped me solve about the value exists?
This was the result that worked perfectly:


Private Sub Form_Open(Cancel As Integer)

If IsNull(CustomerIdctrl) Then
CustomerIdctrl.Visible = False
SellerIdctrl.Visible = True ' No customer record
Else
CustomerIdctrl.Visible = True
SellerIdctrl.Visible = False ' Customer exists
End If
End Sub


Now I'm trying to use it to hide a subreport depending on the same value:


Private Sub Report_Open(Cancel As Integer)

If IsNull(Report.SubProf_cust) Then
Report.SubProf_cust.Visible = False
Report.SubProf_ssel.Visible = True ' No customer record
Else
Report.SubProf_cust.Visible = True
Report.SubProf_ssel.Visible = False ' Customer exists
End If
End Sub


SubProf_cust and SubProf_ssel are the subreports that should hide or show. but it does nothing at all, not even an error. If I take out the "Report." that goes infront of the name, it gives me exactly the same results.

chocobochick
10-28-2005, 09:46 AM
No problem.

First of all, you're not testing whether a value is Null; you're testing to see if a subreport (SubProf_cust) is Null. In the former example, pointing the IsNull function to CustomerIdctrl worked because a control such as a textbox returns the Value Property by default -- it's the same as if you had used IsNull(CustomerIdctrl.Value). In the latter case, you've entered the report object SubProf_cust as the parameter, but a report returns its Controls Collection by default, which will probably never be null. If the value that forms the basis for your condition is available in a control, point it to that control instead.

Also, what exactly does "Report" represent? Is it the exact name of an open report, a declared variable set to an open report or form, or were you attempting to reference the Reports Collection (which is plural)?

ailyn
11-03-2005, 03:08 AM
Yes, but that is because I'm never sure of how to build those querys for the programation. Anyway I already solved it without any vbcode. I simply made the subreports of the same size and put them on top of each other (SubProf_cust on top of course). Now when I get a customer it will block the sellers shipping info from view, just like that. It's not very scientific but it works XD

btw, 'report' just meant report I thought I had to build the query telling th program that those where reports ^.^'