Add property to existing VBA class - class

I would like to do something like add a nice-to-Excel-functions Name property to the WorkBook class. Is there a good way to do this?
More detailed problem: In VBA you can assign a formula to a range in an Excel worksheet. I want to do so, and I want my formula to refer to a second workbook, which is an object called wb in my code. I then use wb.Name in assigning a formula to a range.
The problem arises when wb.Name has a single-quote in it. Then you wind up with something like this:
=MONTH('[Ryan's WB]Sheet1'A1)
in the spreadsheet, which fails because the single-quote in the workbook name matches to the first single-quote.
What I would like is a FunName property for the WorkBook class that replaces all single-quotes in the Name property with two single-quotes and returns that. Then the above formula would properly wind up looking like
=MONTH('[Ryan''s WB]Sheet1'A1)

You don't need to make a separate class to extend the workbook class. You can add properties to the existing ThisWorkbook class module, like this:
Public Property Get FunName() As String
FunName = Replace(Me.Name, "'", "''")
End Property
Then you call ThisWorkbook.FunName to get your cleaned up name. However, this code has to exist in the workbook at hand. If you want it to work on any workbook, your function is the way to go.

Just do a replace to double up the single quotes
WorksheetName = Replace(WB.Name, "'", "''")

What you need is a Class which extends the Workbook object. Insert a class module, then try the following code
Dim WithEvents WB As Workbook
Public Sub SetWB(W As Workbook)
Set WB = W
End Sub
Public Property Get FunName() As String
FunName = Replace(WB.Name, "'", "''")
End Property
Private Sub WB_SheetCalculate(ByVal Sh As Object)
'this runs when WB calculates
End Sub
'use it like this
Dim WB As New wbClass
WB.SetWB ActiveWorkbook
CleanedName = WB.FunName
Note that as a bonus, I've put WithEvents in the line that Dims WB at the top of the class. This allows you to trap all the events that happen to WB, and I included the Calculate event as a demo above. If you are in the class code and click the objects dropdown at top left of the code pane, you'll see the WB object, and if you click this, the right hand list box will give you all the events to choose from.

The final answer appears to be that the WorkBook class can be extended to include a name property that is nice to Excel formulas. This can be done using the method provided by dbb. However, since VBA does not support inheritance, objects of the extended class will have only the properties you define for them.
Therefore, it really makes more sense to just use a function. Here's what I'm going to use:
Function FormulaWorkName(ByVal aName As String) As String
FormulaWorkName = Replace(aName, "'", "''")
End Function
Which I will apply to both worksheet names and workbook names.

Related

Round about Form Copying

Is there a way I can quickly mimic all the properties of a form into for a new form one without directly copying the object (Ctrl-C&V, etc)?
Edit: Looking to copy into an entirely different Access Database.
Open each form in design view, set them equal to an object variable, and then set the properties of the source form equal to the proeprties of the destination form. Use a funciton like this in a public module:
Public Sub UpdateForm(SourceForm as string, DestinationForm as string)
dim frm1 as new form
dim frm2 as new form
set frm1 = forms(SourceForm)
set frm2 = forms(DestinationForm}
'* List the properties you want to copy here:
With form2
.RecordSource = frm1.RecordSource
.Caption = frm1.Caption
.DataEntry = frm1.DataEntry
'* And so on for each property
End With
docmd.save acform, DestinationForm
End Sub
If it's not a one-off project, I'd include some code to check and open the forms if they weren't already open.
File > Options > Object Designers > Form/Report Design View > Form template.
Set the form template to the name of an existing form and that will be used for all new forms.
You can copy a form object using VBA in a slightly more simple way than what DataWriter suggests.
You can use the following statement in the immediate window:
DoCmd.TransferDatabase acExport, "Microsoft Access", CurrentProject.FullName, acForm, "Form1", "Form1_Copy"
Where "Form1" is the name of the source form, and "Form1_Copy" is the name of the destination form.

LibreOffice Basic get Elements from form

I'm trying to get value from textfield on the form.
sub Test(oEv)
oForm = oEv.Source.Model.Parent
textBox = oForm.getByName("Description")
MsgBox textBox.Text
end sub
There is an Exception: "Type: com.sun.star.container.NoSuchElementException" on the line "textBox = oForm.getByName". I have a text field with the name "Description" on the same form, where is the button I press to run this macro. What is wrong here?
Check that the name is the same case, not description.
Also, use the Form Navigator to determine whether the control is under the form in the hierarchy.
Have you tried using an introspection tool such as MRI or XrayTool to view the properties of oForm? With the tool, expand the form to see if it contains the Description control.
Often in Base, it is better to deal with the form as a recordset rather than reading the controls. Here is some sample code:
Sub ButtonClickHandler(oEvent as Object)
'com.sun.star.comp.forms.ODatabaseForm
oForm = oEvent.Source.Model.Parent
lDescriptionCol = oForm.findColumn("DESCRIPTION") ' from underlying query or table
Print(oForm.getString(lDescriptionCol))
BasicLibraries.LoadLibrary("XrayTool")
xray(oForm)
End Sub
For more ideas, see https://forum.openoffice.org/en/forum/viewtopic.php?f=39&t=38725.

Using RecordSource to change form contents

I am trying to create a method that displays a selected table in a DS view form. The form will be used repeatedly to view different tables, so I figured the easiest way would be to create a form and dynamically change the RecordSource based on the user selection.
I created a form called "frmDisplay" to be used to display the contents of a table to the users. I am trying to use this code to update the form.
Private Sub btnViewEntries_Click()
Dim frmDisplay As Form
Dim selection As String
Me.cmboSelection.SetFocus
selection = "tbl" & Me.cmboSelection.Text
Set frmDisplay.RecordSource = selection
DoCmd.OpenForm "frmDisplay", acFormDS
End Sub
Currently, the code is throwing an "Improper use of property" error when I try to set the RecordSource to the selection made in the combo box.
Set frmDisplay.RecordSource = selection
I have looked around for a while now, but I cannot seem to find any definitive answer that applies to this example. Most posts that I found were missing the Set keyword...
Does anyone recognize the problem? More importantly, is this even the best way to make this
work?
Thanks
_____ EDIT _____
I figured it out, see code below.
Dim selection As String
Me.cmboSelection.SetFocus
selection = "SELECT * from tbl" & Me.cmboSelection.Text
DoCmd.OpenForm "frmDisplay", acDesign
Forms!frmDisplay.Form.RecordSource = selection
DoCmd.Close acForm, "frmDisplay", acSaveYes
DoCmd.OpenForm "frmDisplay", acFormDS
I note you've already provided your answer, but you may find the following helpful.
If you have a main form with the controls cmboSelection and btnViewEntries then add a subform control and call it fsbDisplay. Don't put anything in this subform, leave it unbound.
Now try the following one-liner:
Private Sub btnViewEntries_Click()
fsbDisplay.SourceObject = "Table.tbl" & cmboSelection.Value
End Sub
The advantage of this method is that you can use it to call up any table or query (prefix with Query. instead of Table.) and the fields shown are dynamically created.

SSRS - Reference a Textbox Value from a RowGroup

I have a tablix that is using RowGroups to group instances of data. A textbox in the tablix refers to a field as it relates to everything else in that group of data. I would like to create a textbox outside the group, below the tablix, and have it reference the value of that textbox inside the group. I have attempted referencing the group textbox using =ReportItems!GroupTextbox1.Value but this returns empty. Is this in anyway possible in SSRS? Thanks in advance for any assistance anyone can provide.
This scenario is probably not supported, ref http://msdn.microsoft.com/en-us/library/dd255285.aspx
If you provided some more info on what output you are trying to achieve (not just your code) we might be able to help.
Hope this helps someone:
You can use Custom code-feature of the report. Open the Report properties and then Code-tab. In there you can write or own code to save results and read them later on. The variables are in report-scope. Like:
Public Shared Dim MyValue as Double = 0
Public Function SaveMyValue (ByVal s As Double) as Double
MyValue+=s
Return s
End Function
Public Function GetMyValue() as Double
Return MyValue
End Function
And using these functions goes like this, eg. in Textbox expression:
=Code.SaveMyValue(Fields!SomeValue.Value)

How is non-standard worksheet.object created in this Excel VBA

The following VBA 6 in Excel 2000 code
Resides in a form that has text boxes, comboboxes and buttons
One of them is txtUsername, another is txtPassword
--I inherited this code
With shtName
.Unprotect "thepassword"
.range("somenamedrange").Value = cboComboBox.Value
.txtUsername.Text = txtUsername.Text
.txtPassword.Text = txtPassword.Text
...
End With
The code sets a text value for two worksheet objects that appear in the VBA Editor Object list, but are neither defined nor set anywhere else in the Excel Project. Option Explicit is used on all Microsoft Excel Objects, Forms and Modules. I can create procedures for said worksheet objects on said worksheet in the VBA Editor (e.g.,
Private Sub txtUsername_Change()
End Sub
Neither object
worksheet.txtUsername
worksheet.txtPassword
appears nor is set as a named range.
The value of both objects is only used elsewhere by specific reference worksheet.txtUsername.Text
These values do not show up in the locals window after they are set on the worksheet. They are definitely used, as the Essbase queries complete successfully using these objects.
summary:
i understand formName.txtUsername.Text (or .Value)
i do not understand a worksheet object that does not get defined nor instantiated via code
the only bright idea I had was to export the worksheet and view in a text editor, to see if my ancestors created a "custom" worksheet object the way one defines a "default property" in a class module--manually via text editor
(no mention of either property in the worksheet.cls)
Thank you.
Worksheets that are part of the Excel spreadsheet do not have to instantiated, they're part of the workbook and just "always there". If they're not visible to the user but are visible in the project browser, there could be some code in the "ThisWorkbook" section that make the sheets invisible (.visible = false) when the workbook launches.
I found that the VBA Code Cleaner cleaned "references to object definitions that cannot be found".