UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Conditional 'with' Statement, Access 2016    
 
   
Kim
post Sep 19 2019, 01:32 PM
Post#1



Posts: 21
Joined: 25-July 18



Is there anyway to make 'With' statement conditional? I have code that loops through my forms and reports to set up conditional formatting. The controls are mostly the same across multiple forms/reports so I would like to set up something like:

CODE
If iCounter = 1 then
    With Application.Reports("Bid_RevisionSR")
Else
    With Application.Forms("Bid_RevisionSF")
End If

Obviously this will not work because it is the opening line of a nested statement stuck in the middle of another nested statement, but I am wondering if there is a way to get this functionality in another way.

Alternatively, I could include the reference throughout my code and do something like: With Application.Forms(strReportOrFormName).Controls("txtBidDate") but I still need a way to change the Application.Forms reference to Application.Reports
Go to the top of the page
 
MadPiet
post Sep 19 2019, 01:55 PM
Post#2



Posts: 3,327
Joined: 27-February 09



Why not just use two different routines? One accepts a reference to a form, the other accepts a reference to a report. Then you just loop through the controls or whatever, and you're all set.
Go to the top of the page
 
Kim
post Sep 19 2019, 03:00 PM
Post#3



Posts: 21
Joined: 25-July 18



Because they would be fairly redundant. It may come to that, but I would prefer to avoid the redundancy if possible.
Go to the top of the page
 
HansUp
post Sep 19 2019, 03:39 PM
Post#4



Posts: 53
Joined: 19-September 14
From: Colorado USA


Set an object variable to the form or report.

CODE
Dim obj As Object
If iCounter = 1 then
    Set obj = Application.Reports("Bid_RevisionSR")
Else
    Set obj = Application.Forms("Bid_RevisionSF")
End If


Then you can use the object variable to reference a named control it contains.

CODE
With obj
    Debug.Print .Controls("txtBidDate").Value
End With
Go to the top of the page
 
Kim
post Sep 19 2019, 04:51 PM
Post#5



Posts: 21
Joined: 25-July 18



HansUp: Exactly what I was looking for!

Thanks so much!
Go to the top of the page
 


Custom Search


RSSSearch   Top   Lo-Fi    15th October 2019 - 08:44 AM