I am currently writing code in VBA to automate a process that sends out an e-mail that contains a table with information generated by a query in an Access database. I am using HTML to format the table in that e-mail, and I need the colors of the rows in that table to alternate colors (odd rows: #ccffcc; even rows: #ffffff), but I cannot seem to figure it out. I have tried to use the child selector, but I cannot see to figure out to get that to work in here. I do not have much experience or knowledge related to CSS or HTML, so I very well could have been using the child selector incorrectly.
Here is what I currently have:
Bodytext2 = "<html>" & _
"<body>" & _
"<TABLE BORDER=0 CELLpadding=0 style='font-family:Arial; font-size:12px'>" & _
"<TBODY>" & _
"<TH bgcolor='#dcdcdc'> Field1 </TH>" & _
"<TH bgcolor='#dcdcdc'> Field2 </TH>" & _
"<TH bgcolor='#dcdcdc'> Field3 </TH>"
'Creates the table
Set rst = mydb.OpenRecordset("Query In Access")
Do Until rst.EOF
Bodytext2 = Bodytext2 & _
"<TR ALIGN=CENTER VALIGN=MIDDLE>" & _
"<TD> " & rst![Field1] & _
"<TD> " & rst![Field2] & _
"<TD> " & rst![Field3]
rst.MoveNext
Loop
rst.Close
Bodytext2 = Bodytext2 & "</table>"
This code produces the correct table with the correct information. however, I am not sure where to add in the code to change the colors of the table's rows, or how to even write the code to do that. My guess so far is that it would be in the "TR Align=center valign=middle" portion, but that is just my guess.
I have looked a lot of other answers on here, but I think that my lack of knowledge in this field is making it difficult for me to determine what I actually need and where it needs to go. So, if there are helpful answers to similar questions that y'all could direct me to, I would greatly appreciate that too.
Thank You!
Create a counter variable before you go into your loop that will let you keep track of your row number, like this:
i = 1
Do Until rst.EOF
If i Mod 2 = 0 Then
Bodytext2 = Bodytext2 & "<TR ALIGN=CENTER VALIGN=MIDDLE BGCOLOR='#ffffff'>"
Else
Bodytext2 = Bodytext2 & "<TR ALIGN=CENTER VALIGN=MIDDLE BGCOLOR='#ccffcc'>"
End If
Bodytext2 = Bodytext2 & _
"<TD> " & rst![Field1] & _
"<TD> " & rst![Field2] & _
"<TD> " & rst![Field3]
rst.MoveNext
i = i + 1
Loop
My apologies for any syntax errors. My VBA is a little rusty.
Related
I trying to create Add,update,Delete operation on MS Access form.I found this code on internet where Insert and update is happening on the same button. I am not getting what is exactly happening in below line and how it's identifying it is for update or insert.
Not getting following line : = Me.txtid.Tag & "" = ""
Please find below code which works perfect as per requirement.
'When we click on button Add there are two options
'1. for insert
'2. for update
If Me.txtid.Tag & "" = "" Then
' this is for insert new
' add data in table
CurrentDb.Execute "insert into student(stdid,stdname,gender,phone,address)" & _
" values(" & Me.txtid & ",' " & Me.txtname & " ',' " & Me.cmbgender & " ','" & _
Me.txtphone & "', '" & Me.txtaddress & "')"
'refresh data in list on form
subform_student.Form.Requery
Else
CurrentDb.Execute "UPDATE student " & _
" set stdid = " & Me.txtid & _
", stdname = '" & Me.txtname & "' " & _
", gender = '" & Me.cmbgender & " ' " & _
", phone = ' " & Me.txtphone & " ' " & _
", address = ' " & Me.txtphone & " ' " & _
" WHERE stdid = " & Me.txtid.Tag
End If
The .Tag property is a general-purpose string property of every form and control object in VBA/VB6. It is provided as a place for developers to "put stuff" to support the operation of their applications.
The original code from which you copied your sample must have written a value to Me.txtid.Tag when the record was loaded (e.g., perhaps in the form's Current event) to indicate whether the record is an existing record or a new record (empty="new", non-empty="existing"). The line
If Me.txtid.Tag & "" = "" Then
simply checks to see if the .Tag property is empty, and then performs the INSERT or UPDATE accordingly.
BTW, re:
below code which works perfect as per requirement
No, it doesn't. Try adding a record where [stdname] is Tam O'Shanter and see for yourself. You should ditch the dynamic SQL and use one of
a bound form (as Gustav suggests),
a parameterized query, or
a recordset update.
Forget/remove all this code and bind the form to table Student to make this all happen automatically.
If a bound form is not familiar to you, browse for a tutorial for "Beginning with Microsoft Access" or the like.
I have an excel file which contains data for football statistics and I want to be able to change the excel numbers, and in doing so the website updates its table automatically.
Is there a possible way to do this?
This doesn't completely answer your question, but I thought to include it as a partial answer. I have a generic procedure (method) I use from time to time to convert an Excel Table (aka ListObject) into an HTML table. Perl has a module that does this brilliantly, but I've never found anything similar for VBA, hence my hand-rolled code.
The only caveat is that your data has to be in a table. If it's coming from an RDBMS, then the easiest thing to do is to bring it in via MS Query, which automatically renders the output as a Table/ListObject.
I know you are talking about Web and made no mention of VBA -- just take this for what it's worth. I'm hopeful it has some useful components you can glean.
Function TableToHtml(ByRef Table As ListObject, Title As String) As String
Dim row As ListRow
Dim header, col As range
Dim output As String
output = _
"<html>" & vbCrLf & _
" <head>" & vbCrLf & _
" <title>" & vbCrLf & _
Title & vbCrLf & _
" </title>" & vbCrLf & _
" </head>" & vbCrLf & _
"<body>" & vbCrLf & _
"<font size=""5"">" & Title & "</font><br><br>" & vbCrLf & _
"<table border='1px' cellpadding='5' cellspacing='0' style='border: solid 1px Black; font-size: small;'>"
output = output & "<tr align='center' valign='top'>" & vbCrLf
Set header = Table.HeaderRowRange
For Each col In header.Columns
output = output & " <td align='center' valign='top'>" & col.Value & "</td>" & vbCrLf
Next col
output = output & "</tr>" & vbCrLf
For Each row In Table.ListRows
output = output & "<tr align='left' valign='top'>" & vbCrLf
For Each col In row.range.Columns
output = output & " <td align='center' valign='top'>" & col.Value & "</td>" & vbCrLf
Next col
output = output & "</tr>" & vbCrLf
Next row
Dim o As Object
output = output & "<tr align='left' valign='top'>" & vbCrLf
For Each header In Table.TotalsRowRange
output = output & " <td align='center' valign='top'>" & header.Value & "</td>" & vbCrLf
Next header
output = output & "</tr>" & vbCrLf
output = output & "</table>" & vbCrLf & "</body>" & vbCrLf & "</html>"
TableToHtml = output
End Function
Yes, this is pretty brute-force.
<%
Dim objRs
Dim conn
Dim strSearchString
strSearchString = Request.Form("name")
Set objRs = Server.CreateObject("ADODB.recordset")
objRs.CursorLocation = 3
set conn = Server.CreateObject("ADODB.Connection")
conn.open "Data Source=" & Server.Mappath("../db/certs.mdb") & ";Provider=Microsoft.Jet.OLEDB.4.0;"
'replace apostrophe in name to avoid issues
strSearchString = Replace(strSearchString.tostring, "'", "''")
'Sql Query
sql = "Select * FROM [cert] Where [name] like '" & strSearchString & "'"
'open connection
ObjRs.Open sql,conn
'setup the table
with response
.write "<table border=1 width=100% cellspacing=0 cellpadding=0 class=CustomerTable>" & vbcrlf
.write "<tr>"
.write "<th class=AccName colspan=9><div align=center>" & strSearchString & "'s Certifications</div></th></tr>"
.write "<tr>" & vbcrlf
.write "<th class=AccName>Name</th>"
.write "<th class=AccName>Certification</th>"
.write "<th class=AccName>Date Completed</th>"
.write "<th class=AccName>Industry</th>"
.write "<th class=AccName colspan=2>Certification #</th>"
.write "<th class=AccName>Vendor</th>"
.write "<th class=AccName>Date Expires</th>"
.write "<th class=AccName><a href='viewall_sortTechnology.asp'>Technology</a></th>"
.write "</tr>" & vbcrlf
End with
%>
I'm attempting to use the replace function in order to avoid issues with names containing apostrophes. It seems that this isn't working as when I run the page, the output displays only "O's Certifications" instead of "O'Brien's Certifications".
I should note that the code works as expected for any person without an apostrophe in their name.
The back-end database is MS Access.
I'm fairly new to asp, so any assistance here is greatly appreciated.
The safer, better way to do this is to use parameterized queries.
See: Parameterized query in Classic Asp
I'm using VBScript to parse CSV data and display it on a table. I've got the CSV set up and when I open it in Sublime Text, the headers and content are on different lines (there's only one row of content besides the header). It shows two lines in Sublime Text.
The headers don't have a comma at the end of the row so it doesn't break it up and just basically appends the first entry of the second line on to the last entry of the headers line because it's not technically divided by a comma.
The code I'm using is:
<html>
<head>
</head>
<body>
<%#language="vbscript"%>
<table border="1">
<%
dim csv_to_read, fso, act
csv_to_read="sample.csv"
set fso = createobject("scripting.filesystemobject")
set act = fso.opentextfile(server.mappath(csv_to_read))
'Read the first line of the csv, typically these are colum headings
Response.Write "<tr><th>" & replace(act.readline,",","</th><th>") & "</th></tr>" & vbCrLf
'Read the rest of the csv
Response.Write "<tr><td>" & replace(replace(act.readall,vbCrLf,"</td></tr>"&vbCrLf&"<tr><td>"),",","</td><td>") & "</td></tr>"
%>
<caption>Total Number of Records: <%=act.Line-1%></caption>
</table>
</body>
</html>
Why is it not recognizing the line break?
Thanks for your help!
I think your problem is caused by the file not having a EOL marker of vbCrLf (but crLf probably). Evidence:
s = "1,2,3" & vbLf & "4,5,6"
h = "<tr><td>" & replace(replace(s,vbCrLf,"</td></tr>" & vbCrLf & "<tr><td>"),",","</td><td>") & "</td></tr>"
WScript.Echo "vbLf"
WScript.Echo h
s = "1,2,3" & vbCrLf & "4,5,6"
h = "<tr><td>" & replace(replace(s,vbCrLf,"</td></tr>" & vbCrLf & "<tr><td>"),",","</td><td>") & "</td></tr>"
WScript.Echo "vbCrLf"
WScript.Echo h
output:
cscript 23072652.vbs
vbLf
<tr><td>1</td><td>2</td><td>3
4</td><td>5</td><td>6</td></tr>
vbCrLf
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>4</td><td>5</td><td>6</td></tr>
(I hope that the output will prove to the looping advocates that the double replace produces valid HTML)
Update wrt comment:
This version of the script:
s = "1,2,3" & vbCrLf & "4,5,6"
WScript.Echo ".ReadAll() (faked):"
WScript.Echo s
' h = "<tr><td>" & replace(replace(s,vbCrLf,"</td></tr>" & vbCrLf & "<tr><td>"),",","</td><td>") & "</td></tr>"
' step by step
h = replace(s,vbCrLf,"</td></tr>" & vbCrLf & "<tr><td>")
WScript.Echo "step 1 - EOL handling:"
WScript.Echo h
h = replace(h,",","</td><td>")
WScript.Echo "step 2 - comma handling:"
WScript.Echo h
h = "<tr><td>" & h & "</td></tr>"
WScript.Echo "step 3 - head & tail I:"
WScript.Echo h
h = "<!DOCTYPE html><html><head><title>replace demo</title></head><body><table>" & vbCrLf & h & vbCrLf & "</table></body></html>"
WScript.Echo "step 4 - head & tail II:"
WScript.Echo h
output:
cscript 23072652.vbs
.ReadAll() (faked):
1,2,3
4,5,6
step 1 - EOL handling:
1,2,3</td></tr>
<tr><td>4,5,6
step 2 - comma handling:
1</td><td>2</td><td>3</td></tr>
<tr><td>4</td><td>5</td><td>6
step 3 - head & tail I:
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>4</td><td>5</td><td>6</td></tr>
step 4 - head & tail II:
<!DOCTYPE html><html><head><title>replace demo</title></head><body><table>
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>4</td><td>5</td><td>6</td></tr>
</table></body></html>
should make visible how the double replace works and that all parts are important.
Just a quick glance and it looks like you're writing out malformed HTML.
Response.Write "<tr><td>" & replace(replace(act.readall,vbCrLf,"</td></tr>"&vbCrLf&"<tr><td>"),",","</td><td>") & "</td></tr>"
I read the inner replace as replace every carriage return with a closing table cell/table row
The outer replace is replacing the commas with a closing cell/opening cell
You're then appending another closing cell/closing row.
In my opinion, looping through rows, whether or not is more performance efficient is easier to read and debug down the road. If this applies to your situation please consider something like these:
Reading csv file in classic asp. Problem: column values are truncated up to 300 characters
How to read CSV files line by line in VBScript
I am trying to covert spread sheet with hundreds of image path to html pages. For example:
excel sheet
imageId imagePath
1 images/ima1
2 images/ima2
3 images/ima3
... .........
... ........
Are there faster ways to covert these images to html files instead of typing them 1 by 1?
The html files I need is like the following:
<img src=images/ima1 title='ima1' />
<img src=images/ima2 title='ima2' />
<img src=images/ima3 title='ima3' />
Thanks for the help.
There are several ways you can do what you want.
1 . On another sheet, create a formula that will take the information from the first sheet and produce the HTML you want.
="<img src=" & CHAR(34) & INDIRECT("Sheet1!B" &ROW()) & CHAR(34) & " title=" & CHAR(34) & INDIRECT("Sheet1!A" & ROW()) & " />"
Assuming your data is on Sheet1, every row on your second sheet (with that formula in) will contain the html of the same row on Sheet1. (You'd need to adjust the sheet name and columns if there not the same)
Doing this you can then copy and paste everything from the 2nd sheet.
2 . Using a macro to generate the file.
Public Sub GenerateHTML()
Dim Handle As Integer
Dim Sheet As Worksheet
Dim Row As Integer
Set Sheet = ThisWorkbook.ActiveSheet
Row = 2
Handle = FreeFile()
Open "output.html" For Output As Handle
Print #Handle, "<html>" & vbNewLine & "<head>" & vbNewLine & "<title>My Gallery...</title>" & vbNewLine & "</head>" & vbNewLine & "<body>"
Do
If Sheet.Cells(Row, 1) = "" Then
Exit Do
Else
Print #Handle, "<img src=" & Chr(34) & Sheet.Cells(Row, 2) & Chr(34) & " title=" & Chr(34) & "ima" & Sheet.Cells(Row, 1) & Chr(34) & "/>"
Row = Row + 1
End If
Loop
Print #Handle, "</body>" & vbNewLine & "</html>"
Close #Handle
End Sub
This example will create a html file in the same directory as the spreadsheet containing all the links. You'd need to adjust it to suit your needs as its probably not quite right.
I would create a formula in the next column. Assuming that images/ima1 is in B2 this would be something you could put into C2 to get you what you're after and then paste the formula down.
="<img src="&B2&" title='"&RIGHT(B2,LEN(B2)-4)&"' />"