Show the total of a column in the top of a table - vbscript

I have a table and content of the column are coming from a Database. In the bottom of the table I am able to show the total value as well. But I want to show the total at the top of the table because sometimes there are hundreds of Rows, and to see the total I have to scroll to the end.
Is that even possible?
I have tried the below code. Which is giving me the total at the end of the table. But I want to show the total at the top of the table
<Table>
// This will create the column names
// I need to get the Total here above these column names
<tr>
<td> Product </td>
<td> Qty </td>
<td> Price </td>
</tr>
<% set ObjRS =ObjConn.execute ( "select * from pricetable")
total= 0
Do while Not ObjRS.EOf
product = objrs("product")
qty = objrs("Quantity")
price = ObjRS("price")
total = total+ price
response.write "<td>" & product & "</td><td>" & qty & "</td><td>" & price & "</td><td>"
objrs.movenext
loop
response.write "<tr><td> & Total & </tr></td>
</table>

You could just temporarily save the output and then output it in the order you like (I don't know the language you are using there, so there might be some slight changes to make, but in general that would be the most basic solution):
<Table>
// This will create the column names
// I need to get the Total here above these column names
<tr>
<td> Product </td>
<td> Qty </td>
<td> Price </td>
</tr>
<% set ObjRS =ObjConn.execute ( "select * from pricetable")
sum = 0
outText = ""
Do while Not ObjRS.EOf
product = objrs("product")
qty = objrs("Quantity")
price = ObjRS("price")
total = sum + price
outText = outText & "<tr><td>" & product & "</td><td>" & qty & "</td><td>" & price & "</td></tr>"
objrs.movenext
loop
response.write "<tr><td> & total & </td></tr>"
response.write outText
</table>

from
response.write "<tr><td> & total & </td></tr>"
to
response.write "<thead><tr><th> & total & </th></tr></thead>

I would do a second query that calculates the SUM().
Vbscript can have buffer issues if you try to loop through and assign it all to variables — especially for a lot of data. The thead trick is hack-ish and undermines your structure. Best to calculate the sum directly

Related

Loop inside loop with record count for each row of the last loop

I have 3 tables: dbo.Category, dbo.SubCategory and dbo.Product with the following structure:
dbo.Category: (Category_ID, Category_name)
dbo.SubCategory: (SubCategory_ID, Category_ID, SubCategory_name)
dbo.Product: (Product_ID, Category_ID, SubCategory_ID, Product_name)
I need a loop to display all my categories (from the Category table)
I need a second loop (inside the first one) to display all the subcategories belonging to the parent category.
I need to display (with record count) how many products have each one of my subcategories.
For example, I need something like this:
Category 1
--- Subcategory1 (7)
--- Subcategory 2 (11)
Category 2
--- Subcategory 5 (88)
--- Subcategory 9 (36)
Category 3
--- Subcategory 8 (0)
--- Subcategory 22 (122)
I am not familiar with classic asp and vbscript and the only thing I have managed to do until now, is only the first loop with my main Categories as following:
<%
dim Connect,RS_test,sql
Set Connect = Server.Createobject("ADODB.Connection")
Connect.Open = MM_sindesi_STRING
sql = "SELECT * FROM dbo.Category"
set RS_test = Connect.Execute(sql)
%>
<%
Do Until RS_test.Eof
%>
<table width="100%" border="1" cellspacing="1" cellpadding="1">
<tr>
<td><%=RS_test("Category_name")%></td>
</tr>
</table>
<%
RS_test.MoveNext
Loop
%>
<%
Connect.Close()
Set RS_test = Nothing
Set Connect = Nothing
%>
The above code works ok but the only thing I am getting are the names of my categories…
I have tried so hard to find some similar questions in stackoverflow but unfortunately I didn’t managed to find anything…
You use nested Do Loop
<%
set category = server.createobject("adodb.recordset")
SQL="SELECT * FROM dbo.Category"
category.open SQL,Connect,1,3
%>
<%i=0%>
<% Do while not category.eof %>
<%i=i+1%>
<%=i> Category Record
<table width="100%" border="1" cellspacing="1" cellpadding="1">
<tr>
<td><%=category("Category_name")%></td>
</tr>
<%
cat_id=category(Category_ID)
set sub_category = server.createobject("adodb.recordset")
SQL="SELECT * FROM dbo.SubCategory where Category_Id="&cat_id&""
sub_category.open SQL,Connect,1,3%>
<%j=0%>
<% Do while not sub_category.eof %>
<%j=j+1%>
<%=j%> Record Sub Category
<tr>
<td><%=category("SubCategory_name")%></td>
</tr>
<%
sub_category.MoveNext
Loop
%>
</table>
<%
category.MoveNext
Loop
%>

Get data from two last columns in table

I'm trying to retrieve data from all the cells in the two last columns of a table with an unknown number of rows and columns. I have to do this by VBScript which I'm having difficulties with.
In the example below there are 2 rows with each 5 columns. However in my situation, the number of rows and columns varying.
I would like to get the data in the last two columns, being Year 2015 and Year 2016 in first row and 444 and 555 in second row etc. Since the rows and columns are varying I cannot find a fitting script for retrieving the data.
The data should be listed as variables, as I need to parse them into an input field.
<table id="calculations_data" class="key_figures togglable">
<tr>
<th></th>
<th scope="col">Year 2012</th>
<th scope="col">Year 2013</th>
<th scope="col">Year 2014</th>
<th scope="col">Year 2015</th>
<th scope="col">Year 2016</th>
<th></th>
</tr>
<tr id="turnover_data" class="data_row addaptive">
<th class="title">Turnover</th>
<td>111</td>
<td>222</td>
<td>333</td>
<td>444</td>
<td>555</td>
</tr>
</table>
Furthermore the data is located in a website which I access by this script:
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "https://data.biq.dk/users/sign_in"
Note: The website above requires login which I manage via my script. The login details cannot be provided.
I have had success with the code below where the data is located in a tag.
Data_CompanyName = IE.document.getElementsByTagName("h1")(0).innerText
Document.getElementByID("company_name").value = Data_CompanyName
We can extract the innerHTML of that table first and parse that innerHTML as we parse XML. Not sure if this is the best approach but it worked for me.
Try this code:
set objIE = CreateObject("internetexplorer.application")
objIE.visible = true
objIE.navigate "https://data.biq.dk/users/sign_in" '<--make sure the correct path is entered here(the one which takes you to that page containing the table)
while objIE.readyState <>4
Wscript.sleep 1000
Wend
set objTable = objIE.document.getElementById("calculations_data")
strxml = objTable.innerhtml
set objXML = CreateObject("Microsoft.XMLDOM")
objXML.async=False
objXML.loadxml strxml
set objRowNodes = objXML.selectnodes("//tr")
for i=1 to objRowNodes.length-1
tempArr = split(objRowNodes(i).text)
msgbox "second last: "&tempArr(ubound(tempArr)-1)&vbcrlf&_ 'Displays the 2nd last column data
"last: "&tempArr(ubound(tempArr)) 'Displays the last column data
next
Output:

How to iterate visible rows alone using UFT

when i iterate this below webtable,i am getting row count as 3(with hidden row).
but i can see only 2 rows in my application.
i can get row count with help of descriptive programming,but i want to iterate only the rows which are visible.
<table>
<tbody>
<tr class="show">Name</tr>
<tr class="hide">Ticket</tr>
<tr class="show">city</tr>
</tbody>
</table>
i have tried this below code,but its displays hidden row text as well,
for i=1 to rowcount
print oWebtable.getcelldata(i,2)
next
Actual Output-
Name,
Ticket,
city
expecting output-
Name,
city
UFT has no way knowledge of your show/hide class names. If you want to filter out some rows you need to do it yourself.
Set desc = Description.Create()
desc("html tag").Value = "TR"
desc("class").Value = "show"
Set cells = oWebtable.ChildObjects(desc)
Print "Count: " & cells.Count
For i = 0 To cells.Count - 1
Print i & ": " & cells(i).GetROProperty("inner_text")
Next
Note that I had to add TD elements to your table in order for this to work since it's invalid HTML to have text in a TR element.

Data does not display

I've got minor trouble. My data does not display.
My web page script is as follows:
sqlWFlexi = "Select StaffDepart,StaffName From Employee Where StaffNo = '"&myStaffNo &"'"
set rsWFlexi = ConnISAS.execute(sqlWFlexi)
if not rsWFlexi.eof then
myWFlexi = rsWFlexi("StaffName")
end if
sql="Select StaffDepart, StaffName from Employee where StaffNo = '" & myWFlexi & "'"
set rs1 = ConnISAS.execute(sql)
if not rs1.eof then
myDept = rs1("StaffDepart")
myNameStaff= rs1("StaffName")
if rs1("WFlexi") = "-" then
myFlexi = "0"
else
myFlexi = "1"
end if
end if
This is supposed to display StaffName and StaffDepart. Can you tell me what exactly is missing in my script?
You have two sql statement, if
myWFlexi = rsWFlexi("StaffName")get value from
sqlWFlexi = "Select StaffDepart,StaffName From Employee Where StaffNo = '"&myStaffNo &"'"
let just say John Smith
and second statement sql="Select StaffDepart, StaffName from Employee where StaffNo = '" & myWFlexi & "'" get value from
myWFlexi = rsWFlexi("StaffName") that mean you want get the data StaffName and StaffDepart from employee based on the value myWFlexi.
The way you declare it is not right way ,that why it do not display data even you already declare response.write
You have not selected WFlexi in the second sql statement.
Also if you have to print anything then you should either use
<%
response.write myDept
%
>
and if you don't want to use response.write then you have to use asp<%=myDept%> tags in html.

VBA : Querying Access with Excel on server

I'm working with Excel project wich helps to calculate the price of any peace of furniture. The first task is to pick all the materials from the database.
This is the code:
Sub Material_search()
Dim cnt As New ADODB.connection
Dim rst As New ADODB.Recordset
Dim rcArray As Variant
Dim sSQL As String
Dim db_path As String, db_conn As String
Dim item As String
item = Replace(TextBox1.Text, " ", "%") ' Search word
sSQL = "Select Data, NomNr, Preke, Matas, Kaina, Tiek from VazPirkPrekes " & _
"Where VazPirkPrekes.PirkVazID IN (SELECT VazPirkimo.PirkVazID FROM VazPirkimo Where VazPirkimo.Sandelys like '%ALIAVOS')" & _
" and Year(VazPirkPrekes.Data)>=2011 and Preke Like '%" + item + "%' and Kaina > 0" & _
" Order by Preke, Data Desc"
db_path = Sheets("TMP").Range("B6").value
db_conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & db_path & ";"
cnt.Open db_conn
rst.Open sSQL, cnt, adOpenForwardOnly, adLockReadOnly
ListBox1.Clear
If Not rst.EOF Then
rcArray = (rst.GetRows)
rcArray = WorksheetFunction.Transpose(rcArray)
Dim a As Variant
With ListBox1
.ColumnCount = 6
.list = rcArray
.ListIndex = -1
End With
End If
rst.Close: Set rst = Nothing
cnt.Close: Set cnt = Nothing
Label4.Caption = UBound(ListBox1.list) + 1
End Sub
recently I came up with some trouble while querying Access mdb file. The problem is when database file is on local disk, the search works very fast, but when i put database file on server, the search takes 10 times longer, which is not acceptable.
Is there any optimisation for this code ? or is it a server problem
Thanks in advance
That query requires Access' database engine retrieve all 190K rows from both tables. It's not surprising it is slow, and the slowness is compounded when the db engine must retrieve 2 * 190K rows across the network.
If TextBox1.Text contains "foo", this is the statement you're asking the db engine to run:
Select Data, NomNr, Preke, Matas, Kaina, Tiek
from VazPirkPrekes
Where
VazPirkPrekes.PirkVazID IN (
SELECT VazPirkimo.PirkVazID
FROM VazPirkimo
Where VazPirkimo.Sandelys like '%ALIAVOS')
and Year(VazPirkPrekes.Data)>=2011
and Preke Like '%foo%'
and Kaina > 0
Order by Preke, Data Desc
The engine must retrieve all 190K rows from the VazPirkimo table before it can determine which of them include Sandelys values which end with "ALIAVOS". If your selection criterion was for values which start with "ALIAVOS", the engine could use an index on Sandelys to limit the number of rows it must retrieve from VazPirkimo. However, since that approach is probably not an option for you, consider adding a numeric field, Sandelys_group, to VazPirkimo and create an index on Sandelys_group. Give all rows where Sandelys ends with "ALIAVOS" the same Sandelys_group number (1). Then your "IN ()" condition could be this:
SELECT VazPirkimo.PirkVazID
FROM VazPirkimo
Where VazPirkimo.Sandelys_group = 1
The index on Sandelys_group will allow the db engine to retrieve only the matching rows, which will hopefully be a small subset of the 190K rows in the table.
There are other changes you can make to speed up your query. Look at this criterion from your WHERE clause:
Year(VazPirkPrekes.Data)>=2011
That forces the db engine to retrieve all 190K rows from VazPirkPrekes before it can determine which of them are from 2011. With an index on Data, this should be much faster:
VazPirkPrekes.Data >= #2011-01-01# AND VazPirkPrekes.Data < #2012-01-01#
This WHERE criterion will be faster with an index on Kaina:
Kaina > 0
Your ORDER BY begs for indexes on Preke and Data.
Order by Preke, Data Desc
Any or all of those changes could help speed up the query, though I don't know by how much. The killer is this WHERE criterion:
Preke Like '%foo%'
The issue here is similar to the problem with the "Sandelys like" comparison. Since this asks for the rows where Preke contains "foo", rather than starts with "foo", the db engine can't take advantage of an index on Preke to retrieve only the matching rows. It must retrieve all 190K VazPirkPrekes rows to figure out which match. Unless you can use a different criterion for this one, you will be limited as to how much you can speed up the query.
Thanks for the optimization tips, but as I said the problem occurs only when I put data base file on server. And there is not much help from optimization. But I thought about other idea.
The search of empty blank "" returns about 40k records (these records covers everything I need) . So I'm going to put all these records on a distinct sheet on workbook_activate event and later do the query only in that sheet.
Sub Database_upload()
Application.DisplayAlerts = False
On Error Resume Next
Sheets("DATA_BASE").Delete
On Error GoTo 0
Application.DisplayAlerts = False
Sheets.Add
ActiveSheet.name = "DATA_BASE"
Sheets("DATA_BASE").Visible = False: Sheets("DARBALAUKIS").activate
Dim cnt As New ADODB.connection
Dim rcArray As Variant
Dim sSQL As String
Dim db_path As String, db_conn As String
Dim item As String
Dim qQt As QueryTable
item = "" 'search for empty blanks
sSQL = "Select Data, NomNr, Preke, Matas, Kaina, Tiek from VazPirkPrekes " & _
"Where VazPirkPrekes.PirkVazID IN (SELECT VazPirkimo.PirkVazID FROM VazPirkimo Where VazPirkimo.Sandelys like '%ALIAVOS')" & _
" and Year(VazPirkPrekes.Data)>=2011 and Preke Like '%" + item + "%' and Kaina > 0" & _
" Order by Preke, Data Desc"
db_path = Sheets("TMP").Range("B6").value
db_conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & db_path & ";"
db_conn = "ODBC;DSN=MS Access 97 Database;"
db_conn = db_conn & "DBQ=" & db_path
Set qQt = Sheets("Sheet1").QueryTables.Add(connection:=db_conn, Destination:=Sheets("Sheet1").Range("A1"), Sql:=sSQL)
qQt.Refresh BackgroundQuery:=False
End Sub
Results:
Program takes longer on startup, but the search time is acceptable - for me the problem is solved :)

Resources