Why is my vbCrLf not working? - html

I have to declare a variable in which I need to keep multiple sentences (vbscript is the language I am using). So, I put vbCrLf at the end of sentences in order to get a break. But it is not giving me the break instead it is giving me a whole paragraph.
strHtmlBody = "<div><font face='Arial' size='-1'>Greetings "& RS("firstname") &":"& vbCrLf & _
"Here is the email that contains your account information to access your account at www.example.com." & VbCrLf & VbCrLf & _
"Your Email Address: " & RS("Email") & VbCrLf & _
"Your Password : " & RS("Password") & VbCrLf & vbCrLf & _
"Remember, if you have any questions or concerns, please let us know, simply reply to this email." & VbCrLf & _
"ABC Company" & vbCrLf & _
"http://www.example.com" & vbCrLf & vbCrLf
strHtmlBody = strHtmlBody & "</div>"

HTML ignores most whitespace which vbCrLf represents. Use <br> tags in its place.

Related

Structuring HTML so text displays in one paragraph

I'm working on a VBA code in Excel to get input from reports and send emails.
I'm trying to avoid line breaks in my email body.
With olMail
.To = rep
.Subject = title & " - " & EndTitle
'.Recipients.Add rep
.Attachments.Add filePath & "\" & title & " - " & EndTitle & ".pdf"
.htmlBody = "<BODY style=font-size:12pt;font-family:Arial><b><u>Here is an email</u></b></BODY>" & "<BODY style=font-size:12pt;font-family:Arial><b><u>Here is an email</u></b></BODY>" _
& "<br>" & "<b>HK</b>" & "<b>" & amt & "</b>" _
& "<br>" & "Value date" & "<b>" & Trans & "</b>" & "<br>" &
initial
.Display
'.Send
End With
My email looked like this
Here is the email
Here is the email
What I want the email body to look like
Here is the email Here is the email
(on the same line)
Use this instead:
.htmlBody = "<BODY style=font-size:12pt;font-family:Arial><b><u>Here is an emailHere is an email</u></b>" _
& "<br>" & "<b>HK" & amt & "</b>" _
& "<br>" & "Value date" & "<b>" & Trans & "</b>" & "<br>" & initial & "</BODY>"
Or this, depending on which line breaks you want gone:
.htmlBody = "<BODY style=font-size:12pt;font-family:Arial><b><u>Here is an emailHere is an email</u></b>" _
& "<b>HK" & amt & "</b>" _
& "Value date" & "<b>" & Trans & "</b>" & initial & "</BODY>"
See how I encapsulated all the HTML between the opening and closing BODY tags?

Access VBA code : How to define multiple WHERE statements ? UPDATE - SET - WHERE statement

I got following code to work, except for the last line.
So I want to update a table called loggingX , this is working with the code below, except for I want the WHERE clause to not only check for 1 field (this is working) , but I want the WHERE to also check for field WHid to be a fixed value.
I would like to know how I can add multiple parts to my WHERE statement here. UPDATE should only be done if these 2 conditions below are met. I only have trouble and want to know how to put both conditions in the WHERE clause.
stdid=" & Me.txtID.Tag
WHid=" & Me.txtWHid
Complete update statement for current DB (AND is not working):
CurrentDb.Execute "UPDATE loggingX " & _
" SET stdid=" & Me.txtID & _
", stdname='" & Me.txtName & "'" & _
", gender='" & Me.cboGender & "'" & _
", phone='" & Me.txtPhone & "'" & _
", address='" & Me.txtAddress & "'" & _
", WHid='" & Me.txtWHid & "'" & _
" WHERE stdid=" & Me.txtID.Tag
" AND WHid=" & Me.txtWHid
You're missing an ampersand and underscore on the second to last line:
CurrentDb.Execute "UPDATE loggingX " & _
" SET stdid=" & Me.txtID & _
", stdname='" & Me.txtName & "'" & _
", gender='" & Me.cboGender & "'" & _
", phone='" & Me.txtPhone & "'" & _
", address='" & Me.txtAddress & "'" & _
", WHid='" & Me.txtWHid & "'" & _
" WHERE stdid=" & Me.txtID.Tag & _
" AND WHid=" & Me.txtWHid

MS Access using UPDATE statement keeps entering new data

I'm using an UPDATE Statement but whenever I click the Edit button then Update, it's entering a new line but with the same data.
My code:
Private Sub cmdAdd_Click()
'when we click on button Add there are two options
'1. for insert
'2. for update
If Me.txtNumber.Tag & "" = "" Then
'this is for insert new
'add data to table
CurrentDb.Execute "INSERT INTO tblcompany (companyname, companyaddress, contactnumber, contactperson, emailaddress, website, plantlocation, projectinfo, consultant) " & _
" VALUES('" & Me.txtCompanyName & "','" & _
Me.txtCompanyAddress & "','" & Me.txtContactNumber & "','" & _
Me.txtContactPerson & "','" & Me.txtEmailAddress & "','" & _
Me.txtWebsite & "','" & Me.txtPlantLocation & "','" & _
Me.txtProjectInfo & "','" & Me.txtConsultant & "')"
Else
'otherwise (tag of txtNumber store the number of company to be modified)
CurrentDb.Execute "UPDATE tblcompany " & _
" SET companyname='" & Me.txtCompanyName & "''" & _
", companyaddress='" & Me.txtCompanyAddress & "''" & _
", contactnumber='" & Me.txtContactNumber & "'" & _
", contactperson='" & Me.txtContactPerson & "''" & _
", emailaddress='" & Me.txtEmailAddress & "'" & _
", website='" & Me.txtWebsite & "'" & _
", plantlocation='" & Me.txtPlantLocation & "''" & _
", projectinfo='" & Me.txtProjectInfo & "''" & _
", consultant='" & Me.txtConsultant & "''" & _
" WHERE number=" & Me.txtNumber.Tag
End If
'clear form
cmdClear_Click
'refresh data in list on form
frmCompanySub.Form.Requery
End Sub
Isn't the Tag property empty by default? if you're saving a new record you will have to set a tag property equal to the number. So that when you come to update a record the Where number = & me.txt.number.tag is true. Otherwise all record tags of "" will equal "".
Also, tab in on your currentDb.execute line (after then).

run msgbox in background VBscript

Two part question:
I am trying to write a VBscript where a loop runs, but there is a message box that the user can use to abort the sequence at anytime. I know that if you have a sequence with msgbox in it, the script will stop executing until an answer has been received, but can I run it as a subscript, so it doesn't interfere with the main script?
when I use the following script, I never see the msgbox
function test()
msgbox ("test")
end function
wscript.sleep 1000
msgbox "done
i was under the impression that function let you get inputs. Can this even be done with pure vbscript?
Not what I was going for but this is a work around I found. It makes a temporary msgbox that closes itself after a time. Gives the user a 5 second window to abort the sequence each loop.
set infobox = createobject("Wscript.shell")
do while E<N+1
E=E+1
if InfoBox.Popup ("Click cancel to stop sequence", _
5, "Abort Sequence?", 1) = 2 then
E=N+1
end if
loop
The Trick here is to have the first script create and start a second script. This second script will just run in the background and can then wait and kill the initial script Process... This can easily be done with a Function and can be called at the start of your script. When your main script ends, it simply kills the previously created second script. Note: the second script which is created will automatically delete itself upon being run. See the below Script for a good working example:
Dim iKillPID
'Start Kill Script At Start Of Script
iKillPID = KillPID()
For X = 10 To 0 Step -1
WScript.Echo "Closing in " & X & " Seconds"
WScript.Sleep 1000
Next
'Kill The Kill Script At End Of Script
GetObject("winmgmts:root\cimv2:Win32_Process.Handle='" & iKillPID & "'").Terminate
MsgBox "This Script is Complete"
'$$$$$$$$$$
Function KillPID()
Dim strKillScriptPath, strKillCommand, KillFile, StrFileKill, iScriptPID
Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
'Generates a Unique Temp File Name In The Same Directory As The Current Script
strKillScriptPath = objFSO.GetParentFolderName(WScript.ScriptFullName) & Chr(92) & Replace(objFSO.GetTempName, ".tmp", ".vbs")
'Command Line To New Kill Script
strKillCommand = "WScript.exe " & Chr(34) & strKillScriptPath & Chr(34)
'This part gets the Process ID of the Current Running Script
iScriptPID = GetObject("winmgmts:root\cimv2:Win32_Process.Handle='" & _
CreateObject("WScript.Shell").Exec("CMD /C ping 127.0.0.1 -n 2 > nul").ProcessID & "'").ParentProcessID
'String With Kill File Code (Script Process ID Included)
StrFileKill = _
"Const iKillProc = " & iScriptPID & vbCrLf & _
"Dim objFSO: Set objFSO = CreateObject(" & Chr(34) & "Scripting.FileSystemObject" & Chr(34) & ")" & vbCrLf & _
"objFSO.DeleteFile WScript.ScriptFullName, True" & vbCrLf & _ '<-- Deletes itself immediately upon running
"On Error Resume Next" & vbCrLf & _
"Set objKillProc = Nothing" & vbCrLf & _
"Set objKillProc = GetObject(" & Chr(34) & "winmgmts:root\cimv2:Win32_Process.Handle='" & Chr(34) & " & iKillProc & " & Chr(34) & "'" & Chr(34) & ")" & vbCrLf & _
"If objKillProc Is Nothing Then" & vbCrLf & _
" MsgBox " & Chr(34) & "The Process Is Not Running" & Chr(34) & vbCrLf & _
" WScript.Quit" & vbCrLf & _
"End If" & vbCrLf & _
"MsgBox " & Chr(34) & "Click OK To Kill The Script Process" & Chr(34) & vbCrLf & _
"Call KillProcess(iKillProc)" & vbCrLf & _
"WScript.Quit" & vbCrLf & _
"Sub KillProcess(iProcID)" & vbCrLf & _
"Dim objKillProc, strParentProc" & vbCrLf & _
"On Error Resume Next" & vbCrLf & _
"Set objKillProc = Nothing" & vbCrLf & _
"Set objKillProc = GetObject(" & Chr(34) & "winmgmts:root\cimv2:Win32_Process.Handle='" & Chr(34) & " & iProcID & " & Chr(34) & "'" & Chr(34) & ")" & vbCrLf & _
"If Err = 0 And Not objKillProc Is Nothing Then" & vbCrLf & _
" If StrComp(objKillProc.Name, " & Chr(34) & "cmd.exe" & Chr(34) & ", 1) = 0 Or _" & vbCrLf & _
" StrComp(objKillProc.Name, " & Chr(34) & "cscript.exe" & Chr(34) & ", 1) = 0 Or _" & vbCrLf & _
" StrComp(objKillProc.Name, " & Chr(34) & "wscript.exe" & Chr(34) & ", 1) = 0 Then" & vbCrLf & _
" strParentProc = objKillProc.ParentProcessID" & vbCrLf & _
" objKillProc.Terminate()" & vbCrLf & _
" Call KillProcess(strParentProc)" & vbCrLf & _
" End If" & vbCrLf & _
"End If" & vbCrLf & _
"Set strParentProc = Nothing" & vbCrLf & _
"Err.Clear" & vbCrLf & _
"End Sub"
'Write the Code To File
Set KillFile = objFSO.CreateTextFile(strKillScriptPath, True)
KillFile.WriteLine StrFileKill
KillFile.Close
Set KillFile = Nothing
WScript.Sleep 250
'Execute The Script and Return the Script Process ID So You Can Kill It When The Script Ends
KillPID = CreateObject("WScript.Shell").Exec(strKillCommand).ProcessID
End Function
'$$$$$$$$$$
Also, If you're using CScript as the Scripting Engine for your VBS, I believe you can stop the script by pressing CTRL + C in the Command Prompt Window.
Now if your super motivated you can create an HTA that does about the same thing, but present a UserForm or Custom Internet Explorer Window to click and it can also loop through and check if the process is still running and close itself when the script is finished and the process is no longer running. You can add pretty colors and everything too!

How to add a new record from unbound fields of two tables in to a subform

Hey there Good Day can anyone help me please?
I got a mainform and a subform and Ive created a query to join the two tables and ive tried to use the query to insert fields which is unbound, my subform fields which im getting from my join query if I insert into my fields it says and click the Add button I get a error it says:
Run-time error '3134':
Syntax error in insert into statement.
Code I used below:
Private Sub Add_Click()
CurrentDb.Execute "INSERT INTO PlantTransactionQuery(TransactionID,Plant Number,Categories,Description,Location,TransactionDate,Opening_Hours,Closing_Hours,Hours Worked,Fuel,Fuel Cons Fuel/Hours,Hour Meter Replaced,Comments)" & _
"VALUES(" & Me.txt13 & ",'" & Me.txt1 & "','" & Me.txt2 & "','" & Me.txt3 & "','" & Me.txt4 & "','" & Me.txt5 & "','" & Me.txt6 & "','" & Me.txt7 & "','" & Me.txt8 & "','" & Me.txt9 & "','" & Me.txt10 & "'," & Me.txt11 & "," & Me.txt12 & ")"
PlantTransactionsubform.Form.Requery
End Sub
I dont know if u can insert into a query and i dont know which name I must put into brackets for reserved name.
Any help will be much appreciated of how to add two tables in a subform in a button onclick on the same page or even maybe i made a mistake in my code for me everything looks good.
Thanks in advance
You need to put square brackets [] around any table or field name that
contains spaces or "funny characters", or
is an Access reserved word.
Try this instead:
CurrentDb.Execute "INSERT INTO PlantTransactionQuery (TransactionID,[Plant Number],Categories,Description,Location,TransactionDate,Opening_Hours,Closing_Hours,[Hours Worked],Fuel,[Fuel Cons Fuel/Hours],[Hour Meter Replaced],Comments) " & _
"VALUES (" & Me.txt13 & ",'" & Me.txt1 & "','" & Me.txt2 & "','" & Me.txt3 & "','" & Me.txt4 & "','" & Me.txt5 & "','" & Me.txt6 & "','" & Me.txt7 & "','" & Me.txt8 & "','" & Me.txt9 & "','" & Me.txt10 & "'," & Me.txt11 & "," & Me.txt12 & ")"
Edit
You can make SQL statements easier to verify visually if you break them up line-by-line, something like this:
CurrentDb.Execute _
"INSERT INTO [PlantTransactionQuery] (" & _
"[TransactionID], " & _
"[Plant Number], " & _
"[Categories], " & _
"[Description], " & _
"[Location], " & _
"[TransactionDate], " & _
"[Opening_Hours], " & _
"[Closing_Hours], " & _
"[Hours Worked], " & _
"[Fuel], " & _
"[Fuel Cons Fuel/Hours], " & _
"[Hour Meter Replaced], " & _
"[Comments] " & _
") VALUES (" & _
Me.txt13 & ", " & _
"'" & Me.txt1 & "', " & _
"'" & Me.txt2 & "', " & _
"'" & Me.txt3 & "', " & _
"'" & Me.txt4 & "', " & _
"#" & Me.txt5 & "#, " & _
"'" & Me.txt6 & "', " & _
"'" & Me.txt7 & "', " & _
"'" & Me.txt8 & "', " & _
"'" & Me.txt9 & "', " & _
"'" & Me.txt10 & "', " & _
Me.txt11 & ", " & _
Me.txt12 & " " & _
")"
The 3061 error message "Too few parameters..." can happen if a field (column) name is mis-spelled, or a field name that happens to be a reserved word is not enclosed in square brackets. I've edited the query above so all table and column names are bracketed, just to be safe.
Also, check that you are quoting strings and are not quoting numbers. A mistake there could also result in the aforementioned error.
As for [TransactionDate], you probably should un-bind the text box for consistency. Notice that the value for that field is enclosed in hash marks (#) instead of single quotes. You can add a "date" input mask to the text box, or you can use something like...
Format(CDate(Me.txt5), "yyyy-mm-dd")
...to format the date for you.
Finally, be aware that building SQL strings like that leaves you open to SQL injection problems, so you might also consider replacing that .Execute with something like
Dim rst as DAO.Recordset
Set rst = CurrentDB.OpenRecordset("[PlantTransactionQuery]", dbOpenTable)
rst.AddNew
rst![TransactionID] = CLng(Me.txt13)
rst![Plant Number] = Me.txt1
rst![Categories] = Me.txt2
...
rst![Comments] = Me.txt12
rst.Update