Access转换为Json删除访问操作的复杂性,让您为您的应用程序提供更好的数据可用性。
在执行此操作之前,请确保使用的是Microsoft Access 2010或更高版本。 要将Access数据库转换为JSON格式,您需要安装Access转换器。
Option Compare Database Option Explicit ' Convert to JSON button click event Private Sub cmdConvertToJson_Click() Dim strjsonOutput As String ' Use our function to handle conversion strjsonOutput = ConvertToJson() ' Output to the text Box control Me.txtJsonOutput.Value = strjsonOutput End Sub ' Function to convert the current Access table to JSON format Public Function ConvertToJson() As String Dim strsql As String Dim strjsonOutput As String Dim objTable As DAO.Recordset Dim objField As DAO.Field ' Get the current table name from the comboBox control strsql = "SELECT * FROM " & Me.cboTableNames.Value ' Open a Recordset object for the current table Set objTable = CurrentDb.OpenRecordset(strsql,dbOpenSnapshot) ' Start building our JSON string strjsonOutput = "{""data"": [" Do Until objTable.EOF ' Start a new JSON object strjsonOutput = strjsonOutput & "{" ' Loop through each field in the recordset and add it to the JSON object For Each objField In objTable.Fields If objField.Type dbAttachment And Not IsNull(objField.Value) Then strjsonOutput = strjsonOutput & """" & objField.Name & """:""" & objField.Value & """," End If Next objField ' Remove any trailing comma from the JSON object If Right(strjsonOutput,1) = "," Then strjsonOutput = Left(strjsonOutput,Len(strjsonOutput) - 1) End If ' End the current JSON object strjsonOutput = strjsonOutput & "}," ' Move to the next record objTable.MoveNext Loop ' Remove any trailing comma from the JSON string If Right(strjsonOutput," Then strjsonOutput = Left(strjsonOutput,Len(strjsonOutput) - 1) End If ' End the JSON string strjsonOutput = strjsonOutput & "]}" ' Close the recordset and return the JSON string objTable.Close Set objTable = nothing ConvertToJson = strjsonOutput End Function