VBA Executing Sub Oddly - vba

I have made this simple invoice control worksheet in excel and I use VBA to make it easier to visualize and add new items. I have made dozens of other VBA programmed Worksheets, and all of them have a "New" active x button, programmed just like this:
Private Sub ButtNew2_Click()
Dim Guia As Worksheet
Dim UltLin As Integer
Set Guia = Workbooks("Notas Fiscais.xlsm").Worksheets("Saída")
UltLin = Guia.UsedRange.Rows.Count + 1
Guia.Application.Goto Reference:="R" & UltLin & "C1"
FormNotasSaida.Show
FormNotasSaida.BoxDataEmiss.SetFocus
End Sub
Simple as that. Just select the first blank line so that the form loads blank. It works fine in any other Workbook. But in this one, if and every time I click this button, after closing the form, the next time (and only once) I load the form again in any possible way (either double clicking an item, pressing the "Show" button or pressing the "New" button again), it loads either blank or showing the last launched item (case you did so).
After closing it, I can click wherever or press the "Show" button whenever, they work fine, as they always have. The problem occurs exclusively once, after pressing the "New" button.
What am I possibly doing wrong, specially knowing that this method works perfectly in all other workbooks?

FormNotasSaida.Show
FormNotasSaida.BoxDataEmiss.SetFocus
Forms are a special kind of class modules with a designer and a predeclared ID attribute. This "predeclared ID" is what's at play here: you're using/reusing the default global instance of the class, and [unless you close it with the X button in the control box,] never unload it so you're always showing the same form.
A best practice is to create a new instance of the form every time you use it:
Set frm = New FormNotasSaida
frm.Show
frm.BoxDataEmiss.SetFocus

Related

Get value from command button VBA

I have created A userform with few command buttons.
I can't seem to figure out how do I get the information from them.
I want to open this userform from another one and then I want the user to choose an option from one of the buttons which will change a specific cell in the table itself
the userform I created
I did not write anything on this userform therefor there is no code from it to show only the design.
how do get the information from the buttons to be written in A specific cell on a specific worksheet?
double click on one of the buttons, in the code menu a new sub should appear. this looks something like:
Sub CommandButton1_onClick()
End sub
Alongside this event method, it also has a few properties. The one that is usefull here is the CommandButton1.Value, this represents the state of the button (either true or false iirc).
So build for each button (I strongly advice giving them custom names to prevent getting lost in the trouble) as sub like this:
Sub CommandButton1_onClick()
if CommandButton1.Value then
ThisWorkBook.WorkSheets("WorksheetName").Range("YourRange").Value = "Some value"
else
ThisWorkBook.WorkSheets("WorksheetName").Range("YourRange").Value = ""
end if
End sub

How to use VBA to add a button to a sheet and assign its click event upon another button press

I am trying to automate a quarterly patient report for a local pharmacy and in doing so I have transferred it to excel. One portion of the automation is an add patient button on the cover page of the report that goes to a form for relevent information. The ok button on the form takes the information and formats it in a new sheet named according to the patient's name. The button also adds two newly created buttons to the patient sheet, a delete and edit button. I can create the buttons and place them, but I can not find any way to assign a click event to the buttons, since they are considered new objects on each page.
I have moved the button's main code to the workbook itself, so all I really need to put in the button's click event is a call to that method, but I can't find any way to access the new buttons' click events through vba, and since I need to call a method in VBA itself, I'm not sure I can use a macro either (fair note, I am not all that familiar with excel macros, so if the solution lies in them, I can use that too).
Here is the code that instantiates and places/sizes the delete button on the new sheet:
Dim btn As OLEObject
Set btn = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False, DisplayAsIcon:=False)
With btn
.Name = "deletePatientButton"
.Object.Caption = "Delete Patient"
.Top = 5.25
.Left = 290.25
.Width = 75
.Height = 24.75
.PrintObject = False
End With
Here is the main method of the delete button placed in the workbook code itself (note it only really calls another verification form, so this may be redundant, but I wanted to put it in the workbook section for testing since I assumed it would have the largest scope):
Public Sub mainDeleteButton(sheet As Worksheet)
Dim confirmer As New deleteConfirmationForm
sheet.Activate
confirmer.Show
End Sub
Finally, here is an example of the click event I am hoping to be able to place, or replace with another solution:
Private Sub deletePatientButton_Click()
Call ThisWorkbook.mainDeleteButton(Me)
End Sub
Any help is more than appreciated!
It is possible to add the event code programmatically to the worksheet module (see this post). However, it may be easier to keep your buttons on template worksheets that already have the event code in them. Just copy your template to a new sheet, rename it, and add your patient data.

How to launch a Look up form, and return the value from a combo box

Hopefully I can explain what I want to do well enough...here it goes...
I have a data entry form...the user will be entering employeeIDs. Once in normal operation, most people will be entering only their own EmpID, and they should know it, so this won't be a big problem 99% of the time once this DB goes live.
However, I need some temps to enter historical data from paper sheets into the DB. These people will not know anyone else's EmpID. I'd like to set the Student field's OnDblClick event in the subform's datasheet to open a small form with a combo box. The combo box has a list of all Employee Names, and is bound to the EmpID. Once this user enters the name, and selects the person, I have a button they can click to return to the datasheet.
I can use a function to launch the form, no problem there. But how do I return the EmpID to the field in the datasheet that was double clicked?
When user double clicks in the Student field...I want the next form to appear, and then once they type in the name and select the correct person...and then click Found Them!...I need that bound value to return.
I'd love to say I have code to share right now...but the only code I have is to launch the look up form. I'm brain farting on how to pull the value back down.
The way to do this to launch your little dialog form as “acDialog”. This will cause the calling code to WAIT.
And then the “magic” part is when they click on “Found Them” you do NOT close the popup form, but simply set the form’s visible = false. This has the effect of the calling code that popped up this form that halted to continue (the form is kicked out of dialog mode when you do this). So now your calling code continues.
So your code will look like this:
Dim strF As String ' name of popup form
strF = "frmPopUp"
' open form, wait for user selection
DoCmd.OpenForm strF, , , , , acDialog
' if for is NOT open, then assume user hit cancel buttion
' (you should likly have a cancel button on the form - that cancel buttion will
' execute a docmd.close
If CurrentProject.AllForms(strF).IsLoaded = True Then
' grab the value of thee combbo box
strComboBoxValue = Forms(strF)!NameOfComboBox
DoCmd.Close acForm, strF
End If
As noted, the code behind the Found Them button DOES NOT do a close form, but sets forms visible = false (me.Visible = false), and this trick allows the calling code to continue at which point you can examine any value on the form. Remember to then close the form after you grab the value.
It looks like your data table is in a subform so there is a little more work but it does not have to be as complex as the above solution if you don't want it to be. #Andre451 was close but you need the extra step of identifying the form and subform. For the purpose of demonstration let's call the form Attendance and subform Entry then I'll call the second form LookUp. So the code for your double click in the subform field will of course look something like this :
Private Sub Student_DblClick(Cancel As Integer)
DoCmd.OpenForm "LookUp"
End Sub
You really don't need anything else fancy there. For the button on "LookUp" you will put this:
Private Sub Command2_Click()
Forms![Attendance]![Entry].Form![Student] = Forms!Lookup!Student
DoCmd.Close acForm, "LookUp"
End Sub
And that should get you what you want without any overhead or having to leave any ghosts open.

Running a VBA script from a button after selecting a chart

I’m running Excel 2010 on Windows 7.
I have four charts on a worksheet. What I want to do is select one of the charts and then click a button to open the ‘Format Axis’ dialogue box. I found the following code online to open the dialogue box. If I select a chart and then run the code from the toolbar (Developer tab, Macros, select macro, press ‘Run’), it works well.
Sub formatXAxis1()
ActiveChart.Axes(xlCategory).Select
Application.CommandBars.ExecuteMso "ChartFormatSelection"
End Sub
The trouble I have is when the VBA script is assigned to a button. If I use a shape as the button, I get “Run-time error ‘91’: Object variable or With block variable not set”. I get the same error if I use an ActiveX Command Button. However, with a Form Control button it works as expected. The only problem with this solution is that it is not possible to change the background colour of the button so it looks out of place against the other macro calling buttons on the worksheet.
I'm guessing that in the first two cases (shape and ActiveX buttons), VBA drops or loses the chart selection when I press the button – even though the chart still appears selected on screen. Is there a fix for this, or am I doing something wrong?
For an ActiveX button, in the Properties set the property
TakeFocusOnClick
to False. That will cause Selection to keep on the chart rather than switch to the button and your code should work. The color can also be changed from the properties box, though you probably already know that.
You need to reference the chart by name. So either have 4 buttons (one for each chart), or ask the user to input the name of the chart, then:
Dim co as ChartObject
Dim c as Chart
Dim NameOfChart as String
NameOfChart = Inputbox("Enter name of chart")
Set co = ActiveSheet.ChartObjects(NameOfChart)
Set c = co.chart
c.Axes(xlCategory).Select
Application.CommandBars.ExecuteMso "ChartFormatSelection"

How to reset Excel internal button count?

I have an an Excel sheet that uses VBA to generate some form control buttons on the fly.
the buttons are cleared and then new buttons are created.
I have noticed that even though old buttons are deleted Excel is keeping an internal register of each button. New buttons have button name of over 11K
I don't know if there is some sort of limit excel will allow for this and I don't want to run out of buttons.
I am not sure if this growing registry of buttons past is causing the file size to grow.
I would like to be able to reset the increment back to 0
Anyone have any idea how I can go back to button_0 ? (without starting a whole new Excel sheet)
Seems internal button count is sheet specific. Solution is to copy sheet, rename old sheet, then rename new sheet to old sheet name. Then delete old sheet. Viola! button count reset.
I found the answer somewhere in a forum, but I couldn't retrace it.
You could also pragmatically create the button by a function/sub-routine as a workaround.
Example: below function adds a button and limits the count to a fixed number (in my case, it is the total buttons available).
In my sheet, the first button is named "Button 687" (before I use the macro) and the second button is named "Button 2".
But, it is quite not dynamic when you want to add Drop Down or other form control etc. Macro recording helps you figure out the syntax, methods and properties of the form control you want to add though.
I am not sure why "buttons" is not listed in Properties/Methods after you typed "Activesheet." but Activesheet.Buttons(1).Name or Activesheet.buttons.add are valid codes.
Public Sub Add_Button(ButtonLabel$, ButtonSize#, BFontSize#, BFontName$)
Dim Button__ As Object
Dim One_Unit#: Per_Unit = Application.CentimetersToPoints(1)
'Creates a button at the sheet's first cell (row 1, col 1) with the height and width being 1cm.
Set Button__ = ActiveSheet.Buttons.Add(1, 1, One_Unit, One_Unit)
'button count will be restricted to the number set by user.
With Button__
.Name = "Button " & ActiveSheet.Buttons.Count
With .Characters
.Text = UCase(BLabel)
.Font.Name = BFontName
.Font.Size = BFontSize
End With
End With
End Sub
Sub AddAButton()
Add_Button BLabel:="SAVE"
Debug.Print ActiveSheet.Buttons(ActiveSheet.Buttons.Count).Name
End Sub