最佳方法-使用带有多个最终用户的VBA从Excel向Oracle写入记录

问题描述 投票:0回答:1

我已经在带有VBA的Excel中开发了一个应用程序,该应用程序具有一个用户表单,该表单可以从最终用户那里收集数据,并将该数据插入Oracle 11g中的表中。我必须将此文件分发给10-15个用户。我目前正在通过代码连接我的个人凭据,并且一切都按设计工作。

我需要设置哪种配置,以便可以从连接字符串中删除我的用户名和密码,并使所有最终用户仍然可以连接?它们都已经定期连接到该数据源。因此,他们安装了Oracle Client,并具有tnsnames.ora文件等。我只是不知道如何对该应用进行生产化,以便可以对其进行分发?工作代码如下:

    Public Sub SubmitSupplytoOracle(sYear As Integer, sWeek As Integer, sArea As String, sCrew As String, sSuper As String, _
                                    sTECOH As Integer, sTECUG As Integer, sContOH As Integer, sContUG As Integer)

    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim sqlInsert As String
    Dim sCurrUser As String
    Dim sConnString As String
    Dim recordsAffected As Long
    'Dim wsTest As Worksheet
    'Dim sqlSelect As String

    'Set wsTest = ActiveWorkbook.Worksheets("Test") 'have to create a new worksheet called 'Test' to use this
    sCurrUser = CreateObject("WScript.Network").UserName
    sConnString = "User ID=xxxxxx;Password=yyyyyy;Data Source=ZZZZ;Provider=OraOLEDB.Oracle"


    sqlInsert = "INSERT INTO POTW_MH_Supply (" & _
                    "MHSupplyID," & _
                    "SupplyYear," & _
                    "ISO_Week," & _
                    "Service_Area," & _
                    "CrewHQ," & _
                    "Supervisor_Last_Name," & _
                    "TEC_OH_MHs," & _
                    "TEC_UG_MHs," & _
                    "Cont_OH_MHs," & _
                    "Cont_UG_MHs," & _
                    "Insert_UserName)" & _
                    " VALUES(" & _
                        "seq_POTWMHSupply.NEXTVAL," & _
                        sYear & "," & _
                        sWeek & "," & _
                        "'" & sArea & "'," & _
                        "'" & sCrew & "'," & _
                        "'" & sSuper & "'," & _
                        sTECOH & "," & _
                        sTECUG & "," & _
                        sContOH & "," & _
                        sContUG & "," & _
                        "'" & sCurrUser & "'" & _
                        ")"


    'sqlSelect = "SELECT * FROM STJOF.POTW_MH_Supply"


    Set cn = New ADODB.Connection
    Set rs = New ADODB.Recordset

    cn.Open sConnString
    cn.CommandTimeout = 60

    cn.BeginTrans
    cn.Execute sqlInsert, recordsAffected
    cn.CommitTrans

    Debug.Print recordsAffected

    If recordsAffected = 1 Then
        MsgBox "Your submittal of Supply Man Hours was successful"
    Else
        MsgBox "Your submittal might not have been recorded succesfully for some reason. Please try again"
    End If


    'Might use this later if I need to return some values from this table.
    'rs.Open sqlSelect, cn
    'wsTest.Range("B3").CopyFromRecordset rs


    cn.Close

    'Cleanup objects
    Set cn = Nothing
    Set rs = Nothing
    sConnString = ""

    End Sub
excel vba oracle11g adodb tnsnames
1个回答
0
投票

考虑让每个用户将YAML文件保存到指定的安全位置,该位置保存他们的数据库凭据,并让Excel将配置文件解析为键/值对的集合,以完成连接字符串。

YAML

database_connection:
 provider : OraOLEDB.Oracle
 database : ZZZZ
 user     : xxxxxx
 password : yyyyyy

VBA

YAML解析器功能

Function ParseYAML(strFile) As Collection
    Dim myFile As String, textline As String, oneline As String
    Dim dataArray As Variant, data As Variant, key As Variant
    Dim dbColl As Collection
    Dim line As Integer, sizeArray As Integer

    Set dbColl = New Collection

    ' OPEN YAML FILE
    Open strFile For Input As #1

    ' LOOP THROUGH LINES
    Do Until EOF(1)
        Line Input #1, textline

        oneline = Replace(textline, " ", "")
        dataArray = Split(oneline, ":", 2)
        sizeArray = UBound(dataArray, 1) - LBound(dataArray, 1) + 1

        ' POPULATE KEY/VALUE PAIR
        If Not textline = "" And Not sizeArray = 0 Then
            data = dataArray(1)
            key = dataArray(0)

            If Right(key, 1) <> ":" Then
                dbColl.Add data, key
            End If
        End If
    Loop        
    Close #1

    Set ParseYAML = dbColl
End Function

当前宏

Public Sub SubmitSupplytoOracle(...)
    Dim dbCreds As Collection
    ...

    Set dbCreds = ParseYAML("C:\Path\To\OracleConnectionDetails.yml")

    sConnString = "Provider=" & dbCreds("provider") & _
                  ";Data Source=" & dbCreds("database") & _
                  ";User ID=" & dbCreds("user") & _
                  ";Password=" & dbCreds("password") & ";"

    ...

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.