Attribute VB_Name = "MAIN"
Option Explicit

#If Win64 Then
    Private Declare PtrSafe Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
    Private Declare PtrSafe Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long
    Private Declare PtrSafe Function GetCurrentProcess Lib "kernel32" () As Long
    Private Declare PtrSafe Function IsWow64Process Lib "kernel32" (ByVal hProc As Long, bWow64Process As Boolean) As Long

#Else
    Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
    Private Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long
    Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    Private Declare Function IsWow64Process Lib "kernel32" (ByVal hProc As Long, bWow64Process As Boolean) As Long
#End If


Public cSrv, cDb, cUser, cPwd As String
Public nNtUser As Long    'Flag: 0 - Sql Connect, 1 - Win Connect
Public pnMode, pnUser, dbPath, dbName, odbcDriver, pcNameApp, pUser_id, pPermitUpd, pPermitDepends, pnLoadSvod, pnCalc, pnAddress_Row As Long, pnAddress_Col As Long
Public KRIT_ALG_COLUMNCOUNT, KRIT_ALG_COLUMN, KRIT_ALG_ROW, KRIT_FORM_COLUMNCOUNT, KRIT_FORM_COLUMN, KRIT_FORM_ROW, KRIT_SHABLON_COND_COLUMNCOUNT, _
    KRIT_SHABLON_COND_COLUMN, KRIT_SHABLON_COND_ROW, KRIT_SPR_COLUMNCOUNT, KRIT_SPR_COLUMN, KRIT_SPR_ROW, KRIT_SQLDATA1_COLUMNCOUNT, KRIT_SQLDATA1_COLUMN, KRIT_SQLDATA1_ROW, _
        KRIT_STR_COND_COLUMNCOUNT, KRIT_STR_COND_COLUMN, KRIT_STR_COND_ROW, KRIT_GRAF_COLUMNCOUNT, KRIT_GRAF_COLUMN, KRIT_GRAF_ROW, KRIT_RAZDEL_COLUMNCOUNT, KRIT_RAZDEL_COLUMN, KRIT_RAZDEL_ROW, _
        KRIT_ZNAK_COLUMNCOUNT, KRIT_ZNAK_COLUMN, KRIT_ZNAK_ROW, KRIT_TYPE_FORMULA_COLUMNCOUNT, KRIT_TYPE_FORMULA_COLUMN, KRIT_TYPE_FORMULA_ROW As Long
Public pUPDColl() As New VBA.Collection, pINSColl() As New VBA.Collection, _
        pDELColl() As New VBA.Collection, pNotEmptyColl() As New VBA.Collection, _
        pCodeObject, pCorr_in, pCorr_out, pDateBegin, pDateEnd, pbFormCancel As Boolean, pbShablon, _
        pnGrafLINKForFormula As Long, pnCodeItogForFormula, pnCurrentRow As Integer
'---------------------------------------------------------------------------------------------------------------
' Загрузочная процедура
' EdiS\07.10.10\bugs-16042
'---------------------------------------------------------------------------------------------------------------
Public Sub Start_Import()
Dim lcPath As String, count As Integer, i, c, r

' Удаляем reference на DAO
For Each r In Application.VBE.VBProjects(Application.ThisWorkbook.VBProject.Name).References
    IF Not r.IsBroken Then 'Объект Reference представляет допустимую ссылку в реестре.
        If VBA.InStr(1, VBA.UCase(r.Name), "DAO") <> 0 Then
            Application.VBE.VBProjects(Application.ThisWorkbook.VBProject.Name).References.Remove (r)
        End If
    End IF
Next

lcPath = ThisWorkbook.Sheets("Service").Range("A33")
With ThisWorkbook.VBProject.VBComponents
    For Each i In ThisWorkbook.VBProject.VBComponents
        If (i.Name = "INIT_R") Or (i.Name = "sqlServer") Or (i.Name = VBA.Trim(ThisWorkbook.Sheets("Service").Range("A70").Text)) _
           Or (i.Name = "INFILE") Or (i.Name = "COLLECTION_R") Or (i.Name = "StringFunc") Or (i.Name = "CONTROL_R") _
           Or (i.Name = "CALCULATE_CELL_R") Or (i.Name = "SEEK_POP") Or (i.Name = "SERVICE_R") Or (i.Name = "SQLSERVER_R") _
           Or (i.Name = "MODE_CALCULATE") Or (i.Name = "MODE_CONTROL") Or (i.Name = "ADDCOND") Or (i.Name = "INFILE") _
           Or (i.Name = "SQLSERVER") Then
              Set c = i
              ThisWorkbook.VBProject.VBComponents.Remove (c)
        End If
    Next
    If ThisWorkbook.Sheets("Service").Range("A30") = 1 Then
        .Import (lcPath + "sqlServer.bas")
        .Import (lcPath + VBA.Trim(ThisWorkbook.Sheets("Service").Range("A70").Text))
        .Import (lcPath + "INFILE.bas")
    Else
        .Import (lcPath + "SERVICE_R.bas")
        .Import (lcPath + "SQLSERVER_R.bas")
        .Import (lcPath + "MODE_CALCULATE.frm")
        .Import (lcPath + "MODE_CONTROL.frm")
        .Import (lcPath + "ADDCOND.frm")
        If ThisWorkbook.Sheets("Service").Range("A35") <> 0 Then
            .Import (lcPath + "INFILE.bas")
            .Import (lcPath + VBA.Trim(ThisWorkbook.Sheets("Service").Range("A70").Text))
        End If
    End If
    'Общие модули для двух режимов
    .Import (lcPath + "INIT_R.bas")
    .Import (lcPath + "COLLECTION_R.bas")
    .Import (lcPath + "StringFunc.bas")
    .Import (lcPath + "CONTROL_R.bas")
    .Import (lcPath + "CALCULATE_CELL_R.bas")
    #If Win64 Then
        'не грузим форму
    #Else
        .Import (lcPath + "SEEK_POP.frm")
    #End If
End With
    count = Sheets("Service").Range("B23")
    ReDim pUPDColl(count)
    ReDim pINSColl(count)
    ReDim pDELColl(count)
    ReDim pNotEmptyColl(count)
End Sub
' -------------------------------------------------------------------------------------------------------