ms access vba dynamic array -- why won't this work? - vba

I have an access form with a textbox named txtInput and a button name btnAdd.
The following is the sub procedure for the button's click event; each time a user enters some text into the text box and clicks the button it should be adding the text string to the dynamic array.
Public Sub btnAdd_Click()
Dim equipArray() As String
Dim ctr As Integer
ctr = 0
Do While txtInput <> "stop"
ReDim Preserve equipArray(x)
equipArray(x) = txtInput
ctr = ctr + 1
Loop
End Sub
But it's not working, can anyone help please?

It's recreating the array every time you click the button. You're not preserving it until after you're recreating it. The best thing to do would be to put a hidden textbox on the form and write the values to that textbox, and then on some event (form close? whatever you want) you can write the value from that textbox to your table.

Related

Change one interactive textbox based on input of another

I have an interactive textbox in my powerpoint, which I can input text into while presentation mode. I am wondering how I can change the text within another interactive textbox on another slide to what I input in the original interactive textbox?
I have tried some code in vba:
ActivePresentation.Slides(8).Shapes("TextBoxNAME2").TextFrame.TextRange.Text = "TEST"
to no avail. Any ideas?
Concerning your sample
ActivePresentation.Slides(8).Shapes("TextBoxNAME2").OLEFormat.Object.Text = "TEST"
Or maybe this.
If it is an ActiveX Textbox object you may use event handler:
' put this in Slide's module, where You input new value at
' e.g Slide No.1 Module
Private Sub TextBox1_LostFocus()
' You want to update TextBox1 ona Slide 2
Slide2.TextBox1.Text = Me.TextBox1.Text
End Sub

Updating Word Form Field

I have a Word form that I am working on.
It has one field that is supposed to be calculated from other fields.
In the prior iteration, you could click the cell in the table and hit F9 and the field would update.
I have since added some other buttons and VBA and now you can no longer click the cell when "Restrict Editing" is on.
I have tried a button tied to VBA that will update all fields, but when you click that button, you cannot edit any of the fields.
How can I update this field, and still be able to manually update my other fields?
The problem was that Content Controls and ActiveX buttons are not altogether compatible. Also, the programmer I had inherited the form from was using a simple field calculation based on the table in the document instead of VBA. I was able to come up with a better solution. I used the sub:
Private Sub Document_ContentControlOnExit(ByVal thisControl As ContentControl, Cancel As Boolean)
End Sub
to execute code onExit from the controls. This function executes on ALL Content Controls as the user exits the Content Control. Another tool I developed was the following function which will find the index of the control with the given title:
'Function to get control index given the control title
'PARAMETER Control Title as String
'RETURN Control Index as Integer
Public Function GetControlIndex(ccTitle As String) As Integer
'Function Variable Declaration
Dim objCC As ContentControl
'look at each ContentControl
For i = 1 To ActiveDocument.ContentControls.count
Set objCC = ActiveDocument.ContentControls.Item(i)
With objCC
If .Title = ccTitle Then
GetControlIndex = i
End If
End With
Next i
End Function

Looking for a new method to create 'check-in' and 'check-out' system

I am currently working on a database for a small building with a large number of HDDs and the system I have made so far works exactly as my employer desires it to.
My current problem is with some VBA coding using macros to sign in and sign out each hard drive, the hard drives have a row each with a 'check in' and 'check out' button at the end. At current moment the coding works for both of these buttons but I have to write out the code for every single button both 'check in' and 'check out'.
Is there a way to convert the location of the button into a string which would then add itself into the coding and I can put in some sort of array that will auto locate the output of each button for me?
The macro is a simple .Show statement.
My excel sheet is illustrated below. Most of the current cells are programmed with formulas as this system needs to be fairly automated for the less skilled computer users.
Edit:
After clicking the button my initials are added to the end of current time/date. This needs to happen for many buttons all results being in different cells.
My recommendation would be to avoid using a button, and instead override the SelectionChanged event handler for the worksheet. Change the buttons to formatted cells, which when clicked will perform the action you desire.
First, configure your UserForm to return a value. One way to do this is by placing the following code into the code-behind for the UserForm:
Public Property Get Initials() As String
Initials = txtInitials.Text
End Property
Public Property Let Initials(ByVal sInitials As String)
txtInitials.Text = sInitials
End Property
Private Sub btnOk_Click()
Me.Hide
End Sub
Next, add the following code to Sheet1:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim sInitials As String
Dim sDate As String
Dim sTime As String
Dim frmInitials As InitialsForm
If Target.Column = 13 Then
Set frmInitials = New InitialsForm
frmInitials.Show
sInitials = frmInitials.Initials
Unload frmInitials
sDate = Date
sTime = Time
Cells(Target.Row, 6).Value = sDate + " / " + sTime + " " + sInitials
End If
End Sub
When the user clicks on a cell in column 13 (M - "Check Out"), a userform will be displayed asking for the user's initials. Once they have entered them and pressed the OK button, the initials will be added to the end of the timestamp and inserted into column 6 (F) of the same row.

Delete multiple textboxes from list of controls

I have a list of controls created dynamically in a control list. The user has options to add textboxes in the control list and to delete them as well.
I have created the textboxes using the following code:
Dim tb As New TextBox
tb.Name = "Textbox" & counter.ToString
tb.Left = 55
tb.Top = fields
Me.Controls.Add(tb)
MyControls.Add(tb)
counter = counter + 1
So, the textbox names when created are Textbox1, Textbox2 and so on maximum up to Textbox10.
The user can delete textboxes by button clicks one by one. If the user wants to delete these textboxes, counter will run backward and will delete Textbox10 first and then Textbox9 and so on (This is the same as First in First Out).
So, for deleting I tried the following code, but it didn't execute, giving an error. The code is written under Button's Click event of the delete button.
For Each CType(Me.Controls("Textbox" & counter), TextBox) As Control In MyControls
Me.Controls.Remove(...) 'The textbox's name to be deleted in place of dots
'The textbox name to be deleted here with .Dispose()
Next
The error is: "Expression is a value and therefore cannot be the target of an assignment" in the first line of the above code.
How to delete a series of textboxes dynamically?
This line is causing the problem:
For Each CType(Me.Controls("Textbox" & counter), TextBox) As Control In MyControls
CType(Me.Controls("Textbox" & counter), TextBox) resolves to a value, so it cannot be a loop increment variable.
To delete based on a loop, you would need to know how many controls you want to delete. Here is one way:
For i As Integer = 1 To Math.Min(NumberOfControlsToDelete, 10) ' Cap deleting at 10.
' Make sure we don't go below 1.
If counter < 1 Then Continue
' Expected that the control will exist.
Me.Controls.Remove(Controls.Find("TextBox" & counter, True)(0))
' Decrement counter.
counter -= 1
Next
It looks like you're already keeping track of those dynamic buttons by adding them to a List? in this line (your 6th line of code):
MyControls.Add(tb)
As such, simply grab the last entry and remove it, no need to go searching for the control by name:
If MyControls.Count > 0 Then
Dim TB As TextBox = MyControls(MyControls.Count - 1)
MyControls.Remove(TB)
TB.Dispose()
End If
If you want to delete all of them at the same time:
For Each TB As TextBox In MyControls
TB.Dispose()
Next
MyControls.Clear()
Is this what you're looking for? I'm not sure I'm reading your question correctly.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Controls.Remove(Me.Controls.Find(""Textbox" & counter"))
End Sub

VB Avoiding text box that is occupied

I am making an application where there is a form with a column of text boxes that are filled from two different forms with a button on each. when the button of these forms are clicked they input the results into the text boxes in the column.
the problem I am having is that say i click (form2.button 1) three times it will occupy text boxes 1,2 and 3. Now say I want to use (form1.button 1) to input data into text box 4 it will occupy text box 1.
I have each button set up for multiple clicks so I would like to understand how i can have is so say(form2.button 1) is clicked then (form1.button1) will go to 2nd click for example. there are 10 text boxes so I will need it so that they react to how many times each has been clicked.
Assuming the TextBoxes were called TextBox1 thru TextBox10, you could use a sub like this:
Public Sub AddValue(ByVal value As String)
For i As Integer = 1 To 10
Dim tbName As String = "TextBox" & i
Dim matches() As Control = Me.Controls.Find(tbName, True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is TextBox Then
Dim tb As TextBox = DirectCast(matches(0), TextBox)
If tb.Text.Trim.Length = 0 Then
tb.Text = value
Exit Sub
End If
End If
Next
MessageBox.Show("All TextBoxes are already taken!")
End Sub
Why not just store a counter variable in the form that contains the textboxes, say NextTextBoxNumber that increments each time a textbox is filled in and then you know exactly which textbox to go to next based upon the value of this variable?
Also, to continue with #Oded's comment, you can easily accomplish what he meant using something along the lines of If Textbox1.Text <> "" Then ...
Does that make sense??