Open Local JSON file in VBA - json

I have a number of local JSON files which I am trying to open as a string using VBA in order to extract specific information from them into Excel.
I have seen a similar exercise work when the files are accessed by HTTP (using New.WinHTTP.WinHTTPRequest), however when I've tried to adapt this using a FILE:/// prefix it won't work.
Is there a different Excel method I can use to access the string content of the JSON file?
Cheers
Chris

Reading from disk is not really something you would adapt code that reads from a url to do.
You can load it into memory with;
dim hf As integer: hf = freefile
dim data as string
open "c:\bla\bla.bla" for input as #hf
data = input$(LOF(hf), #hf)
close #hf
debug.? data
There are many results for JSON parsing in vba.

Related

Saving to current user desktop location

So I am using VBA-JSON in excel to convert excel data to json file. I have the filepath set to my desktop however I would like other users to be able to run the conversion as well.
I currently have this as a placeholder for the save location:
'change the path below to save where you desire
Set jsonFileExport = jsonFileObject.CreateTextFile("C:\Users\user\Desktop\jsonExample.json", True)
jsonFileExport.WriteLine (JsonConverter.ConvertToJson(jsonItems, Whitespace:=3))
Exit Sub
I've done research and I've seen mentions of .specialfolders but I am uncertain where to put it in my code. Any help would be appreciated.

Converting JSON txt file to XML

We're constructing a network of data and part of that includes modifying a search query from a public website to pull all of the data we want. That data, however, when pulled is stored into a JSON txt file.
Ultimately we want this data to be stored in an Access Database so the next step, we thought, was to convert it to XML so we can have an Excel sheet to import. We found a formatting tool (http:jsonformatter.org). When running the tool we received the following error:
“Microsoft Access has encountered an error processing the XML schema in file ‘Data.xml’,
A document must contain exactly one root element”
I've no idea what this entails or where to start debugging. Are there alternatives we might consider?
The error says that there is more than one root element. Have you validated the XML generated? I looked at the website. I tried to ask via comment but I don't have enough rep but you should post some of your json and xml.
If I am reading your issue correctly, you are converting json to xml format and then to excel?
I would suggest writing some code to consume the json and export the xml files to import.

How to attach documents in ms access using vb6

Good day. I have an Access database with a field using an attachment data type. Is there a way to use Visual Basic 6 to attach my documents, such as images, Word files and PDF files on that database? Specifically on the field using the attachment data type.
Yes, from the LoadFromFile Method.
Snippet below:
Set AttachmentRecordset = RecordSetForTable.Fields("AttachmentFieldName").Value
FileName = "SomePathToAFile"
AttachmentRecordset.AddNew
AttachmentRecordset.Fields("FileData").LoadFromFile (FileName)
AttachmentRecordset.Update

MS Access VBA to run all saved imports together

I am looking for a VBA code to run multiple saved imports in MS Access 2010. I used DoCmd.RunSavedImportExport "*" but gave an error.
I know I am doing something wrong here. Please understand I am a newbie to VBA. I have almost 8 saved imports in .csv formats in a specific location. All I want is to automate it through VBA.
i guess you have taken all other necessary steps already.
loop through the saved import/export and execute them one by one.
something like:
pseudo would be:
loop through the import/export and execute it manually.
in code would be:
Dim i As Integer
For i = 0 To CurrentProject.ImportExportSpecifications.count - 1
Debug.Print CurrentProject.ImportExportSpecifications(i).name
DoCmd.RunSavedImportExport CurrentProject.ImportExportSpecifications(i).name
Next i
EDIT
Your ImportExportSpecifications details are saved as XML format and you can access that information via
CurrentProject.ImportExportSpecifications(i).XML
within the XML you will find the path = "your file.xlsx". Do a string job to extract the path and validate the file ending and implement your code.

Storing PDFs in MS Access Database using Forms

I need to store PDF files in an Access database on a shared drive using a form. I figured out how to do this in tables (using the OLE Object field, then just drag-and-drop) but I would like to do this on a Form that has a Save button. Clicking the save button would store the file (not just a link) in the database. Any ideas on how to do this?
EDIT:
I am using Access 2003, and the DB will be stored on a share drive, so I'm not sure linking to the files will solve the problem.
We have several databases that contain 10's of thousands of documents (pdf, doc, jpg, ...), no problem at all. In Access, we use the following code to upload a binary object to a binary field:
Function LoadFileFromDisk(Bestand, Optional FileName As String = "")
Dim imgByte() As Byte
If FileName = "" Then FileName = strFileName
Open FileName For Binary Lock Read As #1
ReDim imgByte(1 To LOF(1))
Get #1, , imgByte
Close #1
If Not IsEmpty(imgByte) Then Bestand.Value = imgByte
End Function
In this case, Bestand is the field that contains the binary data.
We use MS SQL Server as a backend, but the same should work on an Access backend.
If you used the same concept but upsized to SQL Server- storing PDFs inside of an Image datatype (or varbinary(max)) then you could SEARCH INSIDE THE PDFs using Full Text Search.
I show that Microsoft says you can do this for any file type where you can register an IFILTER product.. and I just was at the Adobe website the other day and say that their Acrobat IFILTER is indeed FREE.
Maybe this will help: ACC2000: Reading, Storing, and Writing Binary Large Objects (BLOBs).
What they do: Read a file in chunks and add it to a blob using a VBA function.
A field of OLE Object, by default would use a Bound Object Frame on the form. Right click on it and you can Insert an object. It comes complete with browsing for the file. Double-click on the field and the actual document will open.
I recommend going with David's advice and link. Unless you have a need to transfer a single file and want all the PDF's included. Size and performance will be an issue.
If security is an issue and the Access file is the only control you have (You are unable to set security on the folder containing all the linked files.), then you would have to embed.