Learning to use AXE library for ASP - json

I'm attempting to use the AXE library to parse some JSON data. In trying to make sure that everything is running correctly I have created a test page. I am getting an error:
Error Type:
Microsoft JScript compilation (0x800A03EA)
Syntax error
json2.asp Line 1
I'm using the code that is in the json2.asp file to test this out:
<script language="javascript" runat="server" src="json2.asp"></script>
<%
dim TestData : set TestData = JSON.parse(join(array( _
"{", _
" ""firstname"": ""Fabio"",", _
" ""lastname"": ""Nagao"",", _
" ""alive"": true,", _
" ""age"": 27,", _
" ""nickname"": ""nagaozen"",", _
" ""fruits"": [", _
" ""banana"",", _
" ""orange"",", _
" ""apple"",", _
" ""papaya"",", _
" ""pineapple""", _
" ],", _
" ""complex"": {", _
" ""real"": 1,", _
" ""imaginary"": 2", _
" }", _
"}" _
)))
Response.write(TestData.firstname & vbNewline) ' prints Fabio
Response.write(TestData.alive & vbNewline) ' prints True
Response.write(TestData.age & vbNewline) ' prints 27
Response.write(TestData.fruits.get(0) & vbNewline) ' prints banana
Response.write(TestData.fruits.get(1) & vbNewline) ' prints orange
Response.write(TestData.complex.real & vbNewline) ' prints 1
Response.write(TestData.complex.imaginary & vbNewline) ' prints 2
' You can also enumerate object properties ...
dim key : for each key in TestData.keys()
Response.write( key & vbNewline )
next
set TestData = nothing
Where am I going wrong?

Related

Deserialize a JSON String where there are no fixed KEY using vb

I have extracted some data from a JSON Stream with one element being an array. The result of this is
"{" & vbCrLf & " ""2019-08-22"": 128.75," & vbCrLf & " ""2019-08-23"": 151.47," & vbCrLf & " ""2019-08-24"": 151.47" & vbCrLf & "}"
I am then trying to separate the 3 values and place them into a database using VB.
Tried parse, deserialize but going round in circles.
Dim uObject = Newtonsoft.Json.Linq.JObject.Parse("{" & vbCrLf & " ""2019-08-22"": 128.75," & vbCrLf & " ""2019-08-23"": 151.47," & vbCrLf & " ""2019-08-24"": 151.47" & vbCrLf & "}")
DEBUG.PRINT(uObject(1, 1)) - This fails
Trying to get the following output
Key1: 2019-08-22 Value1: 128.75
Key2: 2019-08-23 Value1: 151.47
Key3: 2019-08-24 Value1: 151.47
I did a quick test:
Dim JSON_Obj As JObject = JObject.Parse("{" & vbCrLf & " ""2019-08-22"": 128.75," & vbCrLf & " ""2019-08-23"": 151.47," & vbCrLf & " ""2019-08-24"": 151.47" & vbCrLf & "}")
For Each item In JSON_Obj
Debug.Print(String.Format("Key: {0} Value: {1}", item.key, item.value))
Next
'>Key: 2019-08-22 Value: 128.75
'>Key: 2019-08-23 Value: 151.47
'>Key: 2019-08-24 Value: 151.47
If you can't get it by key name, simply get them with array index or something, like JSON_Obj1 = JSON_Obj.Children.ToArray()(0)
With great help from CruelD; my final working code is
Dim JSON_Obj As Newtonsoft.Json.Linq.JObject = Newtonsoft.Json.Linq.JObject.Parse("{" & vbCrLf & " ""2019-08-22"": 128.75," & vbCrLf & " ""2019-08-23"": 151.47," & vbCrLf & " ""2019-08-24"": 151.47" & vbCrLf & "}")
Dim JSON_Data As List(Of JToken) = JSON_Obj.Children().ToList
For Each uItem As JProperty In JSON_Data
Debug.Print(uItem.Name.ToString & " - " & uItem.Value.ToString)
Next
This works a treat.

Apply Bold to Cell Value inside the text string of a HTMLbody

I am having trouble with applying Bold on a part of a sentence for an automatically generated email.
In my VBA script:
Public strBody As String
.HtmlBody = strBody
Dim FormulaCell As Range
Dim FormulaRange As Range
For Each FormulaCell In FormulaRange.Cells
With FormulaCell
I am able to Bold a whole sentence using "< b >" & "**Text Here**" & "< /b >" &
But it seems impossible to apply the same logic to a more complex sentence, or a referenced value.
strBody = "Hello, " & vbNewLine & vbNewLine & _
"Your task : " & Cells(FormulaCell.Row, "B").Value & " with the mention: " & Cells(FormulaCell.Row, "C").Value & " is nearing its Due Date: "
What I would like to Bold in the above sentence is the value of the FormulaCells.
strBody = "Hello, " & vbNewLine & vbNewLine & _
"Your task : " & **Cells(FormulaCell.Row, "B").Value** & " with the mention: " & **Cells(FormulaCell.Row, "C")**.Value & " is nearing its Due Date: "
So it would look something like that in my email:
Hello
Your task : Eat Potatoes with the mention: Potatoes are Delicious is nearing its Due Date:
Is this something that can be done?
edited
strBody = "<p>Hello</p>, " & vbNewLine & vbNewLine & _
"<p>Your task : <b>" & Cells(FormulaCell.Row, "B").Value & _
"</b> with the mention: <b>" & Cells(FormulaCell.Row, "C").Value & _
"</b> is nearing its Due Date: </p>"

Problems in HTA

So I'm trying to use ScriptGUI (from here) for my .bat files, but it doesn't have a file selector, so I attempted to add one.
I just copied and renamed some code around and it seems to work, apart from adding another variable for the subfunction thing.
' build a file selector
ElseIf UCase(strSplit(0)) = "FILE" Then
strHTML = strHTML & Build_File(strLabel,id,Replace(strLabel," ",""),strSplit(2))
' store the batch file in the arrControls array
arrControls(id) = "file,none"
id = id + 1
>
the problem seems to be the code under here "Click_File(" & fiId & "," & fiFilter & ")"
Function Build_File(fiLabel, fiId, fiName, fiFilter)
' Construct a file selector
Dim strHTML
strHTML = "<input class='button' type='button' name='" _
& fiName & "' value='" & fiLabel & "' id='" & fiId _
& "' onClick=" & chr(34) & "Click_File(" & fiId & "," & fiFilter & ")" & chr(34) _
& " onMouseOver=" & chr(34) & fiName & ".className='button btnhov'" & chr(34) _
& " onMouseOut=" & chr(34) & fiName & ".className='button'" & chr(34) _
& ">"
strHTML = strHTML & " <input type='text' readonly='readonly' value='none' name='fi" & fiName & "' id='fi" & fiId & "'/> "
Build_File = strHTML
End Function
>
Sub Click_File(strId, fiFilter)
' open a file selector
set objShell= CreateObject("WSCript.Shell")
myCur = objShell.CurrentDirectory
Dim file
file = GetFileName(myCur, fiFilter)
arrControls(strId) = "file," & file
document.getElementById("fi" & strId).value = file
document.getElementById("fi" & strId).size = Len(file) + 2
End Sub
It tells me I can't use parenthesis when calling a sub.
Any ideas?
EDIT: new problem,
I'm using a script from Rob van der Woude for the open file dialog (top post from here) which is apparently supposed to work in hta but I get an error saying "ActiveX component can't create object: 'UserAccounts.CommonDialog'"
Well, the message isn't wrong. You can't use parentheses when calling a Sub.
Change this line from:
& "' onClick=" & chr(34) & "Click_File(" & fiId & "," & fiFilter & ")" & chr(34) _
to:
& "' onClick=" & chr(34) & "Click_File " & fiId & "," & fiFilter & chr(34) _
and see if that solves your problem.

Run Code error in Access Macro

I am trying to make a macro in Access that will run VBA code. I trying am using the RunCode command to Run a function that will run a VBA function that I have already created but for some reason it says it can't find it and I am really confused why. I have posted a picture of the macro as well as the code below. Any help would be greatly appreciated.
Macro:
http://imgur.com/uankmL0
Code:
Function Totals()
Dim db As Database
Set db = CurrentDb
foldername = CurrentProject.Path & "\GeneralTotals"
deleteTotals = "DELETE * FROM Totals"
totalVerified = "INSERT INTO Totals([TOTAL VERIFIED FORMULARIES], [TOTAL AVAILABLE FOR IMPORT], [TOTAL SHOULD BE IMPORTED], [TOTAL RECENTLY IMPORTED]) " & _
"SELECT A.cnt, B.cnt, C.cnt, D.cnt " & _
"FROM ( " & _
"SELECT COUNT([FORMULARY ID]) as cnt " & _
"FROM VerifiedFormularies " & _
") AS A " & _
", ( " & _
"SELECT COUNT([FORMULARY ID]) as cnt " & _
"FROM ImportMetricsIDs " & _
") as B " & _
", ( " & _
"SELECT COUNT([FORMULARY ID]) as cnt " & _
"FROM ShouldImportMetricsIDsTable " & _
"WHERE [IMPORTSTATUS]= 'Yes' " & _
") AS C " & _
", ( " & _
"SELECT COUNT([LATEST]) as cnt " & _
"FROM VerifiedFormularies " & _
"WHERE [LATEST]= 'Yes' " & _
") AS D "
db.Execute deleteTotals
db.Execute totalVerified
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel5, "Totals", foldername
MsgBox ("Totals have been exported to: " & foldername)
End Function
Your issue most likely depends on location of trigger event and code placement.
How are you triggering the RunTotals macro? With an embedded macro
using the RunMacro action or in VBA using DoCmd.RunMacro?
Where is the Function Totals() placed? It needs to be behind the form
triggering the event or in a module.
Finally, you can avoid using separate objects (RunTotals macro and embedded macro) if in your trigger event, you type into the Property sheet event line "=Totals()" Alternatively, you can simply call the function in VBA with the simple line: Totals. But location as explained in second bullet still applies.

Inserting string variable into sql string in vb.net

I am having a hard time inserting WONum into my sql string.
I have tried using ' and double '' around WONum. Someone also suggested # and [] around it, but nothing is working thus far.
I keep getting the following error: Incorrect syntax near '1577'
WONum value is actually WO-1577 during run time, but when DA.fill is executed I get that error. I starting to think that the dash is doing something in sql that I'm not aware of. Any help would help, because I have to do several more similar functions in my application.
Public Function GetTechTimes(ByVal WONum As String)
Dim strSQL As String = "Select customer_name, workorder_work_to_be_performed, workorder_work_performed, workorder_notes, workorder_warranty_work, workorder_open_date, workorder_status,workorder_completion_date, wo_tech_name, wo_tech_time, wo_parts_description from Customers, workorders, WorkOrder_Technicians, WorkOrder_Parts Where(customer_id = workorder_customer And wo_tech_wo_id = workorder_id And wo_parts_wo_id = workorder_id And workorder_number = " & WONum & ""
Dim DA As New SqlDataAdapter(strSQL, Conn)
Dim DS As New DataSet
DA.Fill(DS, "TechTimes")
Return DS
End Function
Use Sql-Parameters! That will avoid conversion or other issues and - more important - prevents SQL-Injection attacks.
Public Function GetTechTimes(ByVal WONum As String) As DataSet
Dim strSQL As String = "SELECT customer_name, " & Environment.NewLine & _
"workorder_work_to_be_performed," & Environment.NewLine & _
"workorder_work_performed, " & Environment.NewLine & _
"workorder_notes, " & Environment.NewLine & _
"workorder_warranty_work, " & Environment.NewLine & _
"workorder_open_date, " & Environment.NewLine & _
"workorder_status, " & Environment.NewLine & _
"workorder_completion_date," & Environment.NewLine & _
"wo_tech_name, " & Environment.NewLine & _
"wo_tech_time, " & Environment.NewLine & _
"wo_parts_description" & Environment.NewLine & _
"FROM(customers," & Environment.NewLine & _
" workorders," & Environment.NewLine & _
" workorder_technicians," & Environment.NewLine & _
" workorder_parts)" & Environment.NewLine & _
"WHERE customer_id = workorder_customer " & Environment.NewLine & _
"AND wo_tech_wo_id = workorder_id " & Environment.NewLine & _
"AND wo_parts_wo_id = workorder_id " & Environment.NewLine & _
"AND workorder_number = #workorder_number "
Using con = New SqlConnection(YourConnectionString)
Using da = New SqlDataAdapter(strSQL, con)
da.SelectCommand.Parameters.AddWithValue("#workorder_number", WONum)
Dim DS As New DataSet
da.Fill(DS)
Return DS
End Using
End Using
End Function
Note that i've also used Using-statements to ensure that all gets diposed even in case of an exception.
Bye the way, the reason for your exception: you had an opening brace here: Where(customer_id which was never closed.
As long as workorder_number is a string then putting single quote ' around the WONum is all you need.
You won't need # or square brackets.
If it's not working with the single quote then ensure you've identified/isolated your problem correctly. Remove the And workorder_number = " & WONum & "" from the end of your sql and see if it works without that. If not, then your problem isn't in the WONum, it's earlier in the string.