Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Tigereyes78 is offline Novice
    Windows 11 Office 365
    Join Date
    Aug 2023
    Posts
    11

    Importing DVLA API data into table

    Good morning,
    I have the below code which is in Excel, it takes a number plate from one cell, requests data from DVLA and pastes the resulting data in the adjacent cell. I would like to do this in Access but am not sure exactly how.
    I have a form with a text box containing the registration number, I need a button to run the API request and paste the result into my RegRaw table in a new record.

    Any ideas would be gratefully received *Note API key in code has been replaced by XXX as its for personal use only!

    Thank you


    Sub TestDVLA()
    Dim DvlaService As Object
    Dim Response As Object

    API_KEY = "XXX"

    Set DvlaService = CreateObject("MSXML2.XMLHTTP")

    With DvlaService
    .Open "POST", "https://driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1/vehicles", False
    .setRequestHeader "Content-Type", "application/json"
    .setRequestHeader "Accept", "application/json"


    .setRequestHeader "x-api-key", API_KEY
    .send "{""registrationNumber"": """ & Range("A2") & """}"
    Sheets("DVLAdata").Select
    Range("B2") = .responseText
    End With
    End Sub

  2. #2
    Edgar is offline Competent Performer
    Windows 8 Access 2016
    Join Date
    Dec 2022
    Posts
    274
    You can do this:
    Code:
    Sub Test()
    
        Dim DvlaService As Object
        Dim Response As String
        
        API_KEY = "XXX"
        
        Set DvlaService = CreateObject("MSXML2.XMLHTTP")
        
        With DvlaService
            .Open "POST", "https://driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1/vehicles", False
            .setRequestHeader "Content-Type", "application/json"
            .setRequestHeader "Accept", "application/json"
            .setRequestHeader "x-api-key", API_KEY
            .send "{""registrationNumber"": """ & RegistrationNumber & """}"
            Response = .responseText
        End With
        
        CurrentDb.Execute "INSERT INTO RegRaw (ServerResponse) VALUES (" & Response & ");"
    
    End Sub
    I do not know where you're gonna get RegistrationNumber from. I also don't know how your table looks. I also don't know why your Response variable was an object. And I don't know at what point you're defining the API_KEY. As you can see, not much changes. But you will definitely want to parse the response into the corresponding fields. For that to be easy, use the VBA-JSON library from github, look it up. Add it to your Access file. With that you'll be able to write Response("make"), and if you had a field called make in your table, you could add it to the INSERT INTO instruction above

  3. #3
    Tigereyes78 is offline Novice
    Windows 11 Office 365
    Join Date
    Aug 2023
    Posts
    11
    Thank you for the response, the RegistrationNumber is entered on a Vehicles data entry form (a subform on customers form). Customers table is linked to vehicles table on a one-to-many relationship as customers can have more than 1 vehicle. When a new vehicle Registration Number is added I want to have a button that goes to collect that vehicle data.
    The API is hard coded in, I just replaced it with XXX for the purposes of posting here
    I am very new to the API data request task so am really unsure how best to execute this. I am just building the database now so can change tables and field names as needed. I used to do database development but it was years ago so am very rusty and need a little hand holding

    The code above was adapted from an old colleagues code to get the DVLA data into excel and test the API request was working. I now need to get it into my Access database so if there is a better way please advise

    Thank you

  4. #4
    Tigereyes78 is offline Novice
    Windows 11 Office 365
    Join Date
    Aug 2023
    Posts
    11
    If it helps, this is the response I get into my Excel spreadsheet, all in one cell. (using my own reg)

    {"registrationNumber":"LK66ONU","taxStatus":"Taxed ","taxDueDate":"2024-04-01","motStatus":"Valid","make":"NISSAN","yearOfMan ufacture":2016,"engineCapacity":1461,"co2Emissions ":99,"fuelType":"DIESEL","markedForExport":false," colour":"GREY","typeApproval":"M1","dateOfLastV5CI ssued":"2022-04-09","motExpiryDate":"2024-01-24","wheelplan":"2 AXLE RIGID BODY","monthOfFirstRegistration":"2016-10"}

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,938
    That all looks like comma delimited or colon delimited.
    So I would use the Split() function and then populate a record with every other data item (2, 4, 6 etc) as 1,3 & 5 etc are the fieldnames.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    Edgar is offline Competent Performer
    Windows 8 Access 2016
    Join Date
    Dec 2022
    Posts
    274
    I don't know what you're going to store from that json, make a table in your database and post here a sample so I can tell you how to wire it up. We could guess all week for this, better to work from a sample file.

  7. #7
    Tigereyes78 is offline Novice
    Windows 11 Office 365
    Join Date
    Aug 2023
    Posts
    11
    Quote Originally Posted by Edgar View Post
    I don't know what you're going to store from that json, make a table in your database and post here a sample so I can tell you how to wire it up. We could guess all week for this, better to work from a sample file.
    This is the table I have. I am creating a form for it which will be a subform on the customer form. The registrationNumber will be entered but then I need the rest to populate from the API somehow :/

    Click image for larger version. 

Name:	VehicleTable.png 
Views:	24 
Size:	12.6 KB 
ID:	50608

  8. #8
    Edgar is offline Competent Performer
    Windows 8 Access 2016
    Join Date
    Dec 2022
    Posts
    274
    Take a look at the attached file, let me know how it goes
    Attached Files Attached Files

  9. #9
    Tigereyes78 is offline Novice
    Windows 11 Office 365
    Join Date
    Aug 2023
    Posts
    11
    Quote Originally Posted by Edgar View Post
    Take a look at the attached file, let me know how it goes
    You are a superstar! Thank you so much Saved me another migraine day

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,938
    Quote Originally Posted by Edgar View Post
    Take a look at the attached file, let me know how it goes
    @Edgar,
    Any chance of a 2007 (12) compatible version please? I would like to see it working.

    TIA
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  11. #11
    Tigereyes78 is offline Novice
    Windows 11 Office 365
    Join Date
    Aug 2023
    Posts
    11
    Quote Originally Posted by Welshgasman View Post
    @Edgar,
    Any chance of a 2007 (12) compatible version please? I would like to see it working.

    TIA
    Do you have the API key from DVLA? this has to be requested as it is personal to you. The "XXX" in the code needs to be replaced with your key. I would have expected the file shared by Edgar above would be backwards compatible

  12. #12
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,938
    No, and I am not paying for one.
    I can use your data as the string result and walk through the code, just to see it working.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  13. #13
    Tigereyes78 is offline Novice
    Windows 11 Office 365
    Join Date
    Aug 2023
    Posts
    11
    Quote Originally Posted by Welshgasman View Post
    No, and I am not paying for one.
    I can use your data as the string result and walk through the code, just to see it working.
    You dont have to pay for it! It wont work without the API key to request the data from DVLA.
    The API key is free, you just have to request it from DVLA so its personal to you!

  14. #14
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,938
    Quote Originally Posted by Tigereyes78 View Post
    You dont have to pay for it! It wont work without the API key to request the data from DVLA.
    The API key is free, you just have to request it from DVLA so its personal to you!
    OK, thanks.
    I was just going to set strResponse to your data and then walk through the code.
    Would you have a link to request a key please?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  15. #15
    Tigereyes78 is offline Novice
    Windows 11 Office 365
    Join Date
    Aug 2023
    Posts
    11
    Quote Originally Posted by Welshgasman View Post
    OK, thanks.
    I was just going to set strResponse to your data and then walk through the code.
    Would you have a link to request a key please?
    https://developer-portal.driver-vehi...-ves-api-guide

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Importing data into table from a form
    By Jimbola in forum Access
    Replies: 15
    Last Post: 06-04-2018, 02:57 AM
  2. Replies: 9
    Last Post: 12-15-2017, 08:31 PM
  3. conditionally importing table data from sql
    By vinayak36 in forum Access
    Replies: 7
    Last Post: 07-21-2017, 07:21 AM
  4. Importing data from Pivot Table?
    By jstoler in forum Import/Export Data
    Replies: 22
    Last Post: 07-11-2013, 06:58 AM
  5. Importing excel data into table automatically
    By jwalker55 in forum Import/Export Data
    Replies: 2
    Last Post: 01-10-2012, 07:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums