Nested If Then Statements follow hyperlink VBA - vba

I'm relatively new to VBA and trying to use a series of userforms to generate a URL that fills out a separate webform. The webform will be filled in with the information entered into the userform. The 'WEBSITE' is a link to a pre-populated webform template.
I'm having some issues with nesting the If Then statements to trigger the URL generation. Any help would be much appreciated.
If (UserForm1.Tier1 = True Or UserForm1.Tier2 = True) And UserForm1B.CheckBoxN = True Then
If UserFormN.ComboBoxNPT = January And UserFormN.ComboBoxNPG >= 1 Then
ActiveWorkbook.FollowHyperlink ("WEBSITE" & "Vendor Name: " & UserForm1.ComboBoxVN.Value & "%0A" & UserFormN.Label119.Caption & ": " & UserFormN.ComboBoxNPG.Value)
ElseIf UserFormN.ComboBoxNPT = January And UserFormN.ComboBoxNPS >= 1 Then
ActiveWorkbook.FollowHyperlink ("WEBSITE" & "Vendor Name: " & UserForm1.ComboBoxVN.Value & "%0A" & UserFormN.Label120.Caption & ": " & UserFormN.ComboBoxNPS.Value)
End If
End If

Related

VBA at Access - Filter with several criteria, where one criteria can be Null

I got some problems with my vba code.
I got a form at access with a filter.
At the moment the one filter is for start/end date and a Person.
And because i am a noob, there is an other Filter for a cost centre and the start/end date.
I want to combine both, but with the case, cost centre can be Null or not.
My "main" filter looks like this:
Private Sub Befehl51_Click()
If Nz(Me.txtvon, "") = "" Then //StartDate
MsgBox "Bitte Datumsbereich wählen!"
Exit Sub
End If
If Nz(Me.txtbis, "") = "" Then //EndDate
MsgBox "Bitte Datumsbereich wählen!"
Exit Sub
End If
Me.Filter = "[TaetigkeitsDatum] between " & Format(Nz(Me!txtvon, Date),"\#yyyy-mm-dd\#") & "
and " & Format(Nz(Me!txtbis, Date), "\#yyyy-mm-dd\#") & " And " & "[PersonalID] = " & Me.Liste0 & ""
Me.FilterOn = True
End Sub
The syntax for the cost criteria is like:
[TaetigkeitsKostenstellenIDRef] = "Kombinationsfeld145"
Thanks for help!
You're nearly there, there are just some minor SQL syntax errors.
I've written it to separate lines to increase readability and make understanding easier
Me.Filter = "[TaetigkeitsDatum] BETWEEN " & Format(Nz(Me!txtvon, Date),"\#yyyy-mm-dd\#") & " AND " & Format(Nz(Me!txtbis, Date), "\#yyyy-mm-dd\#") & _
" AND [PersonalID] = " & Me.Liste0
If IsNumeric(Me!Kombinationsfeld145) Then
Me.Filter = Me.Filter & " AND [TaetigkeitsKostenstellenIDRef] =" & Me!Kombinationsfeld145
End If
Some of the changes I've made:
Spacing was off. Before every And and variable name, there should be a space.
The " & " between And and a variable name was unnecessary, removed that.
I've only included the comparison on Me!Kombinationsfeld145 if it's numeric. That way, if it's Null or a zero-length string, it's not included in the comparison.

How to reference cell from another sheet in Module - Excel VBA?

I have a macro in an Excel workbook that updates date from Sheet1 into SQL by clicking a button in Sheet3. Sheet3 also have cells used as parameters during the update process.
Currently, the macro is placed in a module and my SQL statement is as follows:
sSQLUpd = "update [table1].[dbo].[Plan] set [Plan_QTY] = " & Plan & "_
where [MacID] = " & Mac & " and [ModelID] = " & Mdl & " and [Date] = " & dt & "_
and DATEPART(year,[Date])= " & Sheets("Sheet3").Cells(3, 3).Value & "_
and DATEPART(month,[Date])= " & Sheets("Sheet3").Cells(3, 6).Value & ""
conn.Execute sSQLUpd
But when I test the code I keep getting "Subscript out of range" in my error handler.
The SQL structure is fine, since I tested it by replacing the parts:
DATEPART(year,[Date])= " & Sheets("Sheet3").Cells(3, 3).Value & "
and
DATEPART(month,[Date])= " & Sheets("Sheet3").Cells(3, 6).Value & "
with actual numbers and the data can pass.
So it's safe to assume that the codes referencing the cells in Sheet3 have issue. Perhaps it doesn't want to play nice when placed in a module?
I even tried different variations as well:
DATEPART(year,[Date])= " & Sheets("Sheet3").range("C3").Value & "
No dice....
Anything I can do to modify it?
Wait... Never mind. I figured out what went wrong. All I needed to do was change
Sheets("Sheet3") into Sheet3 only:
DATEPART(year,[Date])= " & Sheet3.Cells(3, 3).Value & "
Its always the simple stuff that screws with me. :p

vb.net find and replace text inside a textbox in Word

I have this line of code which does a search and replace in a word document:
oDoc.Content.Find.Execute(FindText:="Invoice", ReplaceWith:="Invoice - Paid: " + paid_date, Replace:=word.WdReplace.wdReplaceAll)
Is there any way, i can do the search and replace inside a specific textbox in word rather than the whole document?
Here is one way to go about it.
For Each oCtl As Shape In doc.Shapes
If oCtl.Type = Microsoft.Office.Core.MsoShapeType.msoTextBox Then
oCtl.TextFrame.TextRange.Text.Replace("Invoice", "Invoice - Paid: " + paid_date)
End If
Next
This essentially searches all the text boxes in your document and replaces "Invoice"
As an attempt to find the name of the textbox try this...
For Each oCtl As Shape In doc.Shapes
If oCtl.Type = Microsoft.Office.Core.MsoShapeType.msoTextBox Then
MsgBox("Name: " & oCtl.Name & vbNewLine & "ID: " & oCtl.ID & vbNewLine & "Title: " & oCtl.Title & vbNewLine & "Text: " & oCtl.TextFrame.TextRange.Text)
End If
Next
This should give you unique or identifiable items of every textbox.
then you could just go like this -
If oCtl.Name = "1234" then oCtl.Text.replace(whatever)
or ID or Title or whatever you decide to choose.

How to create a comment box with time and date stamp in Access using VBA

Completely new to VBA and need help with detailed instructions (dummy version for me).
I have a table with various columns and the following columns, specifically:
ReviewerComments
NewComment
I created a form with both of these fields and need to create an Append Comment button that moves the text from NewComment field and appends it to ReviewerComment field and time/date stamps the comments as they are added. I named this button cmdAppendComment.
I had seen someone else post something and I tried, but as I am completely new to this I know I messed it up. Any help is greatly appreciated.
This is what the VBA code looks like right now:
Private Sub cmdAppendComment_Click()
If (IsNull(NewComment.Value)) Then
MsgBox ("Please provide a comment before clicking" & _
"on the Append Comment button.")
Exit Sub
End If
If (IsNull(ReviewerComments.Value)) Then
ReviewerComments.Value = NewComment.Value & " ~ " & _
VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
Else
ReviewerComments.Value = ReviewerComments.Value & _
vbNewLine & vbNewLine & _
NewComment.Value & " ~ " & _
VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
End If
NewComment.Value = ""
End Sub
I have some suggestions for your code:
1. Do not check if a textbox is null but how many characters your textbox has. You should always do it that way because otherwise you tend to get errors.
If (len(Me.NewComment.Value & "") > 0) Then
MsgBox ("Please provide a comment before clicking" & _
"on the Append Comment button.")
Exit Sub
End If
Here you check the length of the string in your textbox. You need to append "" because otherwise you tend to get null-errors or something similar.
2. You forgot to reference the objects in your form correctly. You have your form and in that form you put your textboxes and also your VBA-code. Your can reference all your objects with "Me.[FormObjects]".
The compiler complains that "NewComment.Value" or "ReviewerComment.Value" is not initialized or in other words not dimensioned. With correct reference this should stop.
Private Sub cmdAppendComment_Click()
If (len(Me.NewComment.Value & "") > 0) Then
MsgBox ("Please provide a comment before clicking" & _
"on the Append Comment button.")
Exit Sub
End If
If (IsNull(Me.ReviewerComments.Value)) Then
Me.ReviewerComments.Value = Me.NewComment.Value & " ~ " & _
VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
Else
Me.ReviewerComments.Value = Me.ReviewerComments.Value & _
vbNewLine & vbNewLine & _
Me.NewComment.Value & " ~ " & _
VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
End If
Me.NewComment.Value = ""
End Sub

VB.NET - using textfile as source for menus and textboxes

this is probably a bit tense and I'm not sure if this is possible at all.
But basically, I'm trying to create a small application which contains alot of PowerShell-code which I want to run in an easy matter.
I've managed to create everything myself and it does work. But all of the PowerShell code is manually hardcoded and this gives me a huge disadvantage.
What I was thinking was creating some sort of dynamic structure where I can read a couple of text files (possible a numerous amount of text files) and use these as the source for both the comboboxes and the richtextbox which provovides as the string used to run in PowerShell.
I was thinking something like this:
Combobox - "Choose cmdlet" - Use "menucode.txt" as source
Richtextbox - Use "code.txt" as source
But, the thing is, Powershell snippets need a few arguments in order for them to work.
So I've got a couple of comboboxes and a few textboxes which provides as input for these arguments. And this is done manually as it is right now. So rewriting this small application should also search the textfile for some keywords and have the comboboxes and textboxes to replace those keywords. And I'm not sure how to do this.
So, would this requre a whole lot of textfiles? Or could I use one textfile and separate each PowerShell cmdlet snippets with something? Like some sort of a header?
Right now, I've got this code at the eventhandler (ComboBox_SelectedIndexChanged)
If ComboBoxFunksjon.Text = "Set attribute" Then
TxtBoxUsername.Visible = True
End If
If chkBoxTextfile.Checked = True Then
If txtboxBrowse.Text = "" Then
MsgBox("You haven't choses a textfile as input for usernames")
End If
LabelAttribute.Visible = True
LabelUsername.Visible = False
ComboBoxAttribute.Visible = True
TxtBoxUsername.Visible = False
txtBoxCode.Text = "$users = Get-Content " & txtboxBrowse.Text & vbCrLf & "foreach ($a in $users)" & vbCrLf & "{" & vbCrLf & "Set-QADUser -Identity $a -ObjectAttributes #{" & ComboBoxAttribute.SelectedItem & "='" & TxtBoxValue.Text & "'}" & vbCrLf & "}"
If ComboBoxAttribute.SelectedItem = "Outlook WebAccess" Then
TxtBoxValue.Visible = False
CheckBoxValue.Visible = True
CheckBoxValue.Text = "OWA Enabled?"
txtBoxCode.Text = "$users = Get-Content " & txtboxBrowse.Text & vbCrLf & "foreach ($a in $users)" & vbCrLf & "{" & vbCrLf & "Set-CASMailbox -Identity $a -OWAEnabled" & " " & "$" & CheckBoxValue.Checked & " '}" & vbCrLf & "}"
End If
If ComboBoxAttribute.SelectedItem = "MobileSync" Then
TxtBoxValue.Visible = False
CheckBoxValue.Visible = True
CheckBoxValue.Text = "MobileSync Enabled?"
Dim value
If CheckBoxValue.Checked = True Then
value = "0"
Else
value = "7"
End If
txtBoxCode.Text = "$users = Get-Content " & txtboxBrowse.Text & vbCrLf & "foreach ($a in $users)" & vbCrLf & "{" & vbCrLf & "Set-QADUser -Identity $a -ObjectAttributes #{msExchOmaAdminWirelessEnable='" & value & " '}" & vbCrLf & "}"
End If
Else
LabelAttribute.Visible = True
LabelUsername.Visible = True
ComboBoxAttribute.Visible = True
txtBoxCode.Text = "Set-QADUser -Identity " & TxtBoxUsername.Text & " -ObjectAttributes #{" & ComboBoxAttribute.SelectedItem & "='" & TxtBoxValue.Text & " '}"
If ComboBoxAttribute.SelectedItem = "Outlook WebAccess" Then
TxtBoxValue.Visible = False
CheckBoxValue.Visible = True
CheckBoxValue.Text = "OWA Enabled?"
txtBoxCode.Text = "Set-CASMailbox " & TxtBoxUsername.Text & " -OWAEnabled " & "$" & CheckBoxValue.Checked
End If
If ComboBoxAttribute.SelectedItem = "MobileSync" Then
TxtBoxValue.Visible = False
CheckBoxValue.Visible = True
CheckBoxValue.Text = "MobileSync Enabled?"
Dim value
If CheckBoxValue.Checked = True Then
value = "0"
Else
value = "7"
End If
txtBoxCode.Text = "Set-QADUser " & TxtBoxUsername.Text & " -ObjectAttributes #{msExchOmaAdminWirelessEnable='" & value & "'}"
End If
End If
Now, this snippet above lets me either use a text file as a source for each username used in the powershell snippet. Just so you know :) And I know, this is probably coded as stupidly as it gets. But it does work! :)
It'll probably get fairly horrible to debug when you've been away from it for a year and are trying to figure out how it hangs together, but if you're doing it I'd suggest that this might be a good time to use XML, since you can then create a nice structure to the files easily that will be fairly easy to understand.
<commands>
<command>
<name>Cmd1</name>
<buttonText>NiceName</buttonText>
<parameters>
<parameter>textBox1</parameter>
<parameter>textBox2</parameter>
<parameter>comboBox1</parameter>
</parameters>
<code>your code here</code>
</command>
<command>
...
</command>
</commands>
You'd have to be careful if your script code contains any characters that can interfere with the xml though.