JSON VBA (1 Viewer)

DanielPINO

New member
Local time
Today, 15:46
Joined
Apr 3, 2025
Messages
3
Hi
I need to embed the services tag within the json
I'm attaching the project.


1743636893489.png
 

Attachments

Hi. I gave it a shot. I hope this is what you meant.

1743639777813.png
 

Attachments

Hi. Almost there.
When I shrink the services tag, it leaves out procedures.
It only shrinks the Consultas tag.
Any suggestions?
Any help is appreciated
Thanks
1743657994600.png
 

Attachments

It would be very helpful if the sample data matched the content and all data fields were available for the JSON fields.

Test-Code:
Code:
Private Sub Test()

    Debug.Print GetFacturaJSON

End Sub

Public Function GetFacturaJSON() As String

    Dim FacturaList As Collection
    Dim FacturaJSON As String
  
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
  
    Set db = CurrentDb
    Set rs = db.OpenRecordset("qrytblFacturasJSON")
    Set FacturaList = New Collection
  
    Do While Not rs.EOF
        FacturaList.Add GetFacturaDict(db, rs)
        rs.MoveNext
    Loop
    rs.Close

    If FacturaList.Count > 1 Then ' use List/Collection
        FacturaJSON = JsonConverter.ConvertToJson(FacturaList)
    ElseIf FacturaList.Count = 1 Then ' FActuraDict as first level
        FacturaJSON = JsonConverter.ConvertToJson(FacturaList(1))
    End If
  
    GetFacturaJSON = FacturaJSON

End Function

Private Function GetFacturaDict(ByVal db As DAO.Database, ByVal FacturaRs As DAO.Recordset) As Dictionary

    Dim FacturaDict As Dictionary
    Dim UsuariosList As Collection

    Dim DictRsKeys() As String
    DictRsKeys = Split("numDocumentoIdObligado,numFactura,tipoNota,numNota", ",")
  
    Set FacturaDict = New Dictionary
      
    Dim i As Long
    For i = LBound(DictRsKeys) To UBound(DictRsKeys)
        FacturaDict.Add DictRsKeys(i), FacturaRs.Fields(DictRsKeys(i)).Value
    Next
  
    Set UsuariosList = GetUsuariosCollection(db, FacturaRs!numFactura.Value) '<--- normaly a filter is required .. but won't work with this example data!
  
    FacturaDict.Add "usuarios", UsuariosList
  
    Set GetFacturaDict = FacturaDict

End Function

Private Function GetUsuariosCollection(ByVal db As DAO.Database, ByVal numFactura As String) As Collection
  
    Dim UsuariosList As Collection
    Set UsuariosList = New Collection

    Dim rs As DAO.Recordset
    ' correct logic, but will not work with example data
    'Set rs = db.OpenRecordset("select * from qrytblUsuariosJSON where numFactura = '" & Replace(numFactura, "'", "''") & "'")
    ' => without filter
    Set rs = db.OpenRecordset("qrytblUsuariosJSON")
    Do While Not rs.EOF
        UsuariosList.Add GetUsuariosDict(db, rs)
        rs.MoveNext
    Loop
    rs.Close
  
    Set GetUsuariosCollection = UsuariosList
  
End Function

Private Function GetUsuariosDict(ByVal db As DAO.Database, ByVal UsuariosRs As DAO.Recordset)
  
    Dim DictRsKeys() As String
    DictRsKeys = Split("tipoDocumentoIdentificacion,numDocumentoIdentificacion,tipoUsuario,fechaNacimiento,codSexo,codPaisResidencia,codPaisOrigen" & _
                       ",codMunicipioResidencia,codZonaTerritorialResidencia,incapacidad,consecutivo", ",")

    Dim UsuariosDict As Dictionary
    Set UsuariosDict = New Dictionary
  
    Dim i As Long
    For i = LBound(DictRsKeys) To UBound(DictRsKeys)
        UsuariosDict.Add DictRsKeys(i), UsuariosRs.Fields(DictRsKeys(i)).Value
    Next
  
    ' servicios
    UsuariosDict.Add "servicios", GetServiciosDict(db, UsuariosRs!numFactura.Value)  ' ... no List/Collection?

    Set GetUsuariosDict = UsuariosDict

End Function

Private Function GetServiciosDict(ByVal db As DAO.Database, ByVal numFactura As String)
  
    Dim DictRsKeys() As String
    DictRsKeys = Split("tipoDocumentoIdentificacion,numDocumentoIdentificacion,tipoUsuario,fechaNacimiento,codSexo,codPaisResidencia,codPaisOrigen" & _
                       ",codMunicipioResidencia,codZonaTerritorialResidencia,incapacidad,consecutivo", ",")

    Dim ServiciosDict As Dictionary
    Set ServiciosDict = New Dictionary
  
  
    ' consultas
    ServiciosDict.Add "consultas", GetConsultasCollection(db, numFactura)
  
    ' procedimientos
    ServiciosDict.Add "procedimientos", GetProcedimientosCollection(db, numFactura)

    Set GetServiciosDict = ServiciosDict

End Function

Private Function GetConsultasCollection(ByVal db As DAO.Database, ByVal numFactura As String) As Collection
  
    Dim ConsultasList As Collection
    Set ConsultasList = New Collection

    Dim rs As DAO.Recordset
    ' correct logic, but will not work with example data
    'Set rs = db.OpenRecordset("select * from qrytblConsultasJSON where numFactura = '" & Replace(numFactura, "'", "''") & "'")
    ' => without filter
    Set rs = db.OpenRecordset("qrytblConsultasJSON")
    Do While Not rs.EOF
        ConsultasList.Add GetConsultasDict(db, rs)
        rs.MoveNext
    Loop
    rs.Close
  
    Set GetConsultasCollection = ConsultasList
  
End Function

Private Function GetConsultasDict(ByVal db As DAO.Database, ByVal ConsultasRs As DAO.Recordset)
  
    Dim DictRsKeys() As String
    DictRsKeys = Split("codPrestador,fechaInicioAtencion,numAutorizacion,codConsulta,modalidadGrupoServicioTecSal,grupoServicios" & _
                       ",codServicio,finalidadTecnologiaSalud,causaMotivoAtencion,codDiagnosticoPrincipal,codDiagnosticoRelacionado1" & _
                       ",codDiagnosticoRelacionado2,codDiagnosticoRelacionado3,tipoDiagnosticoPrincipal,tipoDocumentoIdentificacion" & _
                       ",numDocumentoIdentificacion,vrServicio,conceptoRecaudo,valorPagoModerador,numFEVPagoModerador,consecutivo", ",")

    Dim ConsultasDict As Dictionary
    Set ConsultasDict = New Dictionary
  
    Dim i As Long
    For i = LBound(DictRsKeys) To UBound(DictRsKeys)
        ConsultasDict.Add DictRsKeys(i), ConsultasRs.Fields(DictRsKeys(i)).Value
    Next

    Set GetConsultasDict = ConsultasDict

End Function

Private Function GetProcedimientosCollection(ByVal db As DAO.Database, ByVal numFactura As String) As Collection
  
    Dim ProcedimientosList As Collection
    Set ProcedimientosList = New Collection

    Dim rs As DAO.Recordset
    ' correct logic, but will not work with example data
    'Set rs = db.OpenRecordset("select * from qrytblProcedimientosJSON where numFactura = '" & Replace(numFactura, "'", "''") & "'")
    ' => without filter
    Set rs = db.OpenRecordset("qrytblProcedimientosJSON")
    Do While Not rs.EOF
        ProcedimientosList.Add GetProcedimientosDict(db, rs)
        rs.MoveNext
    Loop
    rs.Close
  
    Set GetProcedimientosCollection = ProcedimientosList
  
End Function

Private Function GetProcedimientosDict(ByVal db As DAO.Database, ByVal ProcedimientosRs As DAO.Recordset)
  
    Dim DictRsKeys() As String
    'DictRsKeys = Split("codPrestador,fechaInicioAtencion,idMIPRES,numAutorizacion,codProcedimiento,viaIngresoServicioSalud,modalidadGrupoServicioTecSal" & _
                       ",grupoServicios,codServicio,finalidadTecnologiaSalud,codDiagnosticoPrincipal,codDiagnosticoRelacionado" & _
                       ",codComplicacion,tipoDocumentoIdentificacion,numDocumentoIdentificacion,vrServicio,conceptoRecaudo,valorPagoModerador" & _
                       ",numFEVPagoModerador,consecutivo", ",")

    DictRsKeys = Split("codPrestador,fechaInicioAtencion,idMIPRES,numAutorizacion,viaIngresoServicioSalud,modalidadGrupoServicioTecSal" & _
                       ",grupoServicios,codServicio,finalidadTecnologiaSalud,codDiagnosticoPrincipal,codDiagnosticoRelacionado" & _
                       ",codComplicacion,tipoDocumentoIdentificacion,numDocumentoIdentificacion,vrServicio,conceptoRecaudo,valorPagoModerador" & _
                       ",numFEVPagoModerador,consecutivo", ",")

    Dim ProcedimientosDict As Dictionary
    Set ProcedimientosDict = New Dictionary
  
    Dim i As Long
    For i = LBound(DictRsKeys) To UBound(DictRsKeys)
        ProcedimientosDict.Add DictRsKeys(i), ProcedimientosRs.Fields(DictRsKeys(i)).Value
    Next

    Set GetProcedimientosDict = ProcedimientosDict

End Function

Note:
I intentionally left the included code repetition to better show the logic.
The Get*Dict procedures can be standardized.
 
Last edited:
It would be very helpful if the sample data matched the content and all data fields were available for the JSON fields.

Test-Code:
Code:
Private Sub Test()

    Debug.Print GetFacturaJSON

End Sub

Public Function GetFacturaJSON() As String

    Dim FacturaList As Collection
    Dim FacturaJSON As String
 
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
 
    Set db = CurrentDb
    Set rs = db.OpenRecordset("qrytblFacturasJSON")
    Set FacturaList = New Collection
 
    Do While Not rs.EOF
        FacturaList.Add GetFacturaDict(db, rs)
        rs.MoveNext
    Loop
    rs.Close

    If FacturaList.Count > 1 Then ' use List/Collection
        FacturaJSON = JsonConverter.ConvertToJson(FacturaList)
    ElseIf FacturaList.Count = 1 Then ' FActuraDict as first level
        FacturaJSON = JsonConverter.ConvertToJson(FacturaList(1))
    End If
 
    GetFacturaJSON = FacturaJSON

End Function

Private Function GetFacturaDict(ByVal db As DAO.Database, ByVal FacturaRs As DAO.Recordset) As Dictionary

    Dim FacturaDict As Dictionary
    Dim UsuariosList As Collection

    Dim DictRsKeys() As String
    DictRsKeys = Split("numDocumentoIdObligado,numFactura,tipoNota,numNota", ",")
 
    Set FacturaDict = New Dictionary
    
    Dim i As Long
    For i = LBound(DictRsKeys) To UBound(DictRsKeys)
        FacturaDict.Add DictRsKeys(i), FacturaRs.Fields(DictRsKeys(i)).Value
    Next
 
    Set UsuariosList = GetUsuariosCollection(db, FacturaRs!numFactura.Value) '<--- normaly a filter is required .. but won't work with this example data!
 
    FacturaDict.Add "usuarios", UsuariosList
 
    Set GetFacturaDict = FacturaDict

End Function

Private Function GetUsuariosCollection(ByVal db As DAO.Database, ByVal numFactura As String) As Collection
 
    Dim UsuariosList As Collection
    Set UsuariosList = New Collection

    Dim rs As DAO.Recordset
    ' correct logic, but will not work with example data
    'Set rs = db.OpenRecordset("select * from qrytblUsuariosJSON where numFactura = '" & Replace(numFactura, "'", "''") & "'")
    ' => without filter
    Set rs = db.OpenRecordset("qrytblUsuariosJSON")
    Do While Not rs.EOF
        UsuariosList.Add GetUsuariosDict(db, rs)
        rs.MoveNext
    Loop
    rs.Close
 
    Set GetUsuariosCollection = UsuariosList
 
End Function

Private Function GetUsuariosDict(ByVal db As DAO.Database, ByVal UsuariosRs As DAO.Recordset)
 
    Dim DictRsKeys() As String
    DictRsKeys = Split("tipoDocumentoIdentificacion,numDocumentoIdentificacion,tipoUsuario,fechaNacimiento,codSexo,codPaisResidencia,codPaisOrigen" & _
                       ",codMunicipioResidencia,codZonaTerritorialResidencia,incapacidad,consecutivo", ",")

    Dim UsuariosDict As Dictionary
    Set UsuariosDict = New Dictionary
 
    Dim i As Long
    For i = LBound(DictRsKeys) To UBound(DictRsKeys)
        UsuariosDict.Add DictRsKeys(i), UsuariosRs.Fields(DictRsKeys(i)).Value
    Next
 
    ' servicios
    UsuariosDict.Add "servicios", GetServiciosDict(db, UsuariosRs!numFactura.Value)  ' ... no List/Collection?

    Set GetUsuariosDict = UsuariosDict

End Function

Private Function GetServiciosDict(ByVal db As DAO.Database, ByVal numFactura As String)
 
    Dim DictRsKeys() As String
    DictRsKeys = Split("tipoDocumentoIdentificacion,numDocumentoIdentificacion,tipoUsuario,fechaNacimiento,codSexo,codPaisResidencia,codPaisOrigen" & _
                       ",codMunicipioResidencia,codZonaTerritorialResidencia,incapacidad,consecutivo", ",")

    Dim ServiciosDict As Dictionary
    Set ServiciosDict = New Dictionary
 
 
    ' consultas
    ServiciosDict.Add "consultas", GetConsultasCollection(db, numFactura)
 
    ' procedimientos
    ServiciosDict.Add "procedimientos", GetProcedimientosCollection(db, numFactura)

    Set GetServiciosDict = ServiciosDict

End Function

Private Function GetConsultasCollection(ByVal db As DAO.Database, ByVal numFactura As String) As Collection
 
    Dim ConsultasList As Collection
    Set ConsultasList = New Collection

    Dim rs As DAO.Recordset
    ' correct logic, but will not work with example data
    'Set rs = db.OpenRecordset("select * from qrytblConsultasJSON where numFactura = '" & Replace(numFactura, "'", "''") & "'")
    ' => without filter
    Set rs = db.OpenRecordset("qrytblConsultasJSON")
    Do While Not rs.EOF
        ConsultasList.Add GetConsultasDict(db, rs)
        rs.MoveNext
    Loop
    rs.Close
 
    Set GetConsultasCollection = ConsultasList
 
End Function

Private Function GetConsultasDict(ByVal db As DAO.Database, ByVal ConsultasRs As DAO.Recordset)
 
    Dim DictRsKeys() As String
    DictRsKeys = Split("codPrestador,fechaInicioAtencion,numAutorizacion,codConsulta,modalidadGrupoServicioTecSal,grupoServicios" & _
                       ",codServicio,finalidadTecnologiaSalud,causaMotivoAtencion,codDiagnosticoPrincipal,codDiagnosticoRelacionado1" & _
                       ",codDiagnosticoRelacionado2,codDiagnosticoRelacionado3,tipoDiagnosticoPrincipal,tipoDocumentoIdentificacion" & _
                       ",numDocumentoIdentificacion,vrServicio,conceptoRecaudo,valorPagoModerador,numFEVPagoModerador,consecutivo", ",")

    Dim ConsultasDict As Dictionary
    Set ConsultasDict = New Dictionary
 
    Dim i As Long
    For i = LBound(DictRsKeys) To UBound(DictRsKeys)
        ConsultasDict.Add DictRsKeys(i), ConsultasRs.Fields(DictRsKeys(i)).Value
    Next

    Set GetConsultasDict = ConsultasDict

End Function

Private Function GetProcedimientosCollection(ByVal db As DAO.Database, ByVal numFactura As String) As Collection
 
    Dim ProcedimientosList As Collection
    Set ProcedimientosList = New Collection

    Dim rs As DAO.Recordset
    ' correct logic, but will not work with example data
    'Set rs = db.OpenRecordset("select * from qrytblProcedimientosJSON where numFactura = '" & Replace(numFactura, "'", "''") & "'")
    ' => without filter
    Set rs = db.OpenRecordset("qrytblProcedimientosJSON")
    Do While Not rs.EOF
        ProcedimientosList.Add GetProcedimientosDict(db, rs)
        rs.MoveNext
    Loop
    rs.Close
 
    Set GetProcedimientosCollection = ProcedimientosList
 
End Function

Private Function GetProcedimientosDict(ByVal db As DAO.Database, ByVal ProcedimientosRs As DAO.Recordset)
 
    Dim DictRsKeys() As String
    'DictRsKeys = Split("codPrestador,fechaInicioAtencion,idMIPRES,numAutorizacion,codProcedimiento,viaIngresoServicioSalud,modalidadGrupoServicioTecSal" & _
                       ",grupoServicios,codServicio,finalidadTecnologiaSalud,codDiagnosticoPrincipal,codDiagnosticoRelacionado" & _
                       ",codComplicacion,tipoDocumentoIdentificacion,numDocumentoIdentificacion,vrServicio,conceptoRecaudo,valorPagoModerador" & _
                       ",numFEVPagoModerador,consecutivo", ",")

    DictRsKeys = Split("codPrestador,fechaInicioAtencion,idMIPRES,numAutorizacion,viaIngresoServicioSalud,modalidadGrupoServicioTecSal" & _
                       ",grupoServicios,codServicio,finalidadTecnologiaSalud,codDiagnosticoPrincipal,codDiagnosticoRelacionado" & _
                       ",codComplicacion,tipoDocumentoIdentificacion,numDocumentoIdentificacion,vrServicio,conceptoRecaudo,valorPagoModerador" & _
                       ",numFEVPagoModerador,consecutivo", ",")

    Dim ProcedimientosDict As Dictionary
    Set ProcedimientosDict = New Dictionary
 
    Dim i As Long
    For i = LBound(DictRsKeys) To UBound(DictRsKeys)
        ProcedimientosDict.Add DictRsKeys(i), ProcedimientosRs.Fields(DictRsKeys(i)).Value
    Next

    Set GetProcedimientosDict = ProcedimientosDict

End Function

Note:
I intentionally left the included code repetition to better show the logic.
The Get*Dict procedures can be standardized.
Testing the code
Hello
It works perfectly with just one user.

When I create a second user, all services (consultations and procedures) are assigned to each user.
How do I filter services by document number?
numDocumentoIdentificacion.Value
So that I can assign only their consultations and procedures to each user?

Thanks for you help !
 
Have you read the code?
Code:
    ' correct logic, but will not work with example data
    'Set rs = db.OpenRecordset("select * from qrytblUsuariosJSON where numFactura = '" & Replace(numFactura, "'", "''") & "'")
    ' => without filter
    Set rs = db.OpenRecordset("qrytblUsuariosJSON")
I assumed that numFactura or similar would serve for join the tables. Unfortunately, there are no corresponding constraints in the sample database. The values in the various tables did not match either. So I didn't give it a second thought.
If you have understood the content of the code shown, customizing the filters should not be a big problem. ;)
 
Last edited:

Users who are viewing this thread

Back
Top Bottom