如何在宏不执行时显示表单?

时间:2015-06-26 00:58:40

标签: excel vba excel-vba ms-access

我正在使用Microsoft Access数据库。我通过Access的子集(使用Microsoft Excel对象库)将几个查询导出到Excel模板。

现在,模板在工作表启动时打开,并开始(从Access)复制其他工作表中的数据。

这是我的问题。我希望在Access将数据复制到我的工作簿时显示一个表单。我不知道该怎么做。

数据完成复制到我的工作簿后,我有一个第一个宏。执行此操作显示表单。但我想在复制过程中显示表格。

总结,

  1. Access打开模板。
  2. 访问开始复制查询
  3. 我的问题......我看到的是工作表,但不是我的表格。
  4. 访问已完成以复制数据
  5. 从Access我执行My Macro
  6. 我的宏在运行时看到我的表单
  7. 我的表单隐藏了,我已准备好工作表。
  8. 导出数据的代码(Access):

    Public Sub CreateExcelInfo()
    'Set reference to Microsoft Excel Object library
    'Set reference to Microsoft ActiveX DataObject 2.x
    
    Dim oExcel As New Excel.Application
    Dim WB As New Excel.Workbook
    Dim WS As Excel.Worksheet
    Dim rng As Excel.Range
    Dim objRs As New ADODB.Recordset
    Dim objRs2 As New ADODB.Recordset
    Dim objRs3 As New ADODB.Recordset
    Dim objRs4 As New ADODB.Recordset
    Dim objRs5 As New ADODB.Recordset
    Dim objRs6 As New ADODB.Recordset
    Dim objRs7 As New ADODB.Recordset
    Dim sSQL, sSQL2, sSQL3, sSQL4, sSQL5, sSQL6 As Variant
    Dim sSQL7 As String
    MsgBox ("Este proceso puede tardar unos minutos." & Chr(13) & Chr(13) & Chr(13) & "Por favor abstengase de realizar tareas en el equipo hasta ver el mensaje de finalizado."), vbOKOnly
    sFileNameTemplate = Application.CurrentProject.Path & "\templates\Informes.xlsm"
    sSQL = "SELECT [VALOR POR TRABAJO EXPORTAR].*FROM [VALOR POR TRABAJO EXPORTAR] " 'This has to be the name of the query your report is using to display data
    sSQL2 = "SELECT [DETALLE REPROCESOS Y GARANTÍAS PARA EXPORTAR].*FROM [DETALLE REPROCESOS Y GARANTÍAS PARA EXPORTAR] " 'This has to be the name of the query your report is using to display data
    sSQL3 = "SELECT [DETALLE HISTORICO PARA EXPORTAR].*FROM [DETALLE HISTORICO PARA EXPORTAR] " 'This has to be the name of the query your report is using to display data
    sSQL4 = "SELECT Clientes.NombreCompañía FROM EMPRESA LEFT JOIN Clientes ON EMPRESA.Empresa = Clientes.IdCliente " 'Export the name register in table EMPRESA
    sSQL5 = "SELECT [COMENTARIOS ÚLTIMO CIERRE].TipoComentarioInventario, [COMENTARIOS ÚLTIMO CIERRE].Comentario FROM [COMENTARIOS ÚLTIMO CIERRE] " 'Export comments last inventory
    sSQL6 = "SELECT [_Indicadores_Pintura].Indicador, HISTORY.Valor FROM (HISTORY RIGHT JOIN [ÚLTIMO INVENTARIO DE CIERRE] ON HISTORY.IdInventario = [ÚLTIMO INVENTARIO DE CIERRE].ÚltimoDeIdInventario) LEFT JOIN _Indicadores_Pintura ON HISTORY.IdIndicador = [_Indicadores_Pintura].IdIndicador WHERE (((HISTORY.IdIndicador)=1 Or (HISTORY.IdIndicador)=2 Or (HISTORY.IdIndicador)=8 Or (HISTORY.IdIndicador)=9 Or (HISTORY.IdIndicador)=10 Or (HISTORY.IdIndicador)=12 Or (HISTORY.IdIndicador)=13 Or (HISTORY.IdIndicador)=14 Or (HISTORY.IdIndicador)=15 Or (HISTORY.IdIndicador)=16 Or (HISTORY.IdIndicador)=17 Or (HISTORY.IdIndicador)=18 Or (HISTORY.IdIndicador)=19 Or (HISTORY.IdIndicador)=20 Or (HISTORY.IdIndicador)=21)) " 'Export data of last inventory
    sSQL7 = "SELECT INVENTARIOS.Observaciones FROM [ÚLTIMO INVENTARIO DE CIERRE] LEFT JOIN INVENTARIOS ON [ÚLTIMO INVENTARIO DE CIERRE].ÚltimoDeIdInventario = INVENTARIOS.IdInventario " 'Export inventory name
    With oExcel
    .Visible = True
                Set WB = .Workbooks.Add(sFileNameTemplate)
                    With WB
                         Set WS = WB.Worksheets("DetalleTrabajos")
                         With WS
                                  objRs.Open sSQL, CurrentProject.Connection, adOpenDynamic, adLockReadOnly
                                  Set rng = .Range("A2")
                                  rng.CopyFromRecordset objRs
                                  objRs.Close
                         End With
                    End With
                    With WB
                         Set WS = WB.Worksheets("DetalleReprocesosYGarantías")
                         With WS
                                  objRs2.Open sSQL2, CurrentProject.Connection, adOpenDynamic, adLockReadOnly
                                  Set rng = .Range("A2")
                                  rng.CopyFromRecordset objRs2
                                  objRs2.Close
                         End With
                    End With
                    With WB
                         Set WS = WB.Worksheets("DetalleHistorico")
                         With WS
                                  objRs3.Open sSQL3, CurrentProject.Connection, adOpenDynamic, adLockReadOnly
                                  Set rng = .Range("A2")
                                  rng.CopyFromRecordset objRs3
                                  objRs3.Close
                         End With
                    End With
                    With WB
                         Set WS = WB.Worksheets("Indicadores Globales")
                         With WS
                                  objRs7.Open sSQL7, CurrentProject.Connection, adOpenDynamic, adLockReadOnly
                                  Set rng = .Range("C7")
                                  rng.CopyFromRecordset objRs7
                                  objRs7.Close
                         End With
                         With WS
                                  objRs6.Open sSQL6, CurrentProject.Connection, adOpenDynamic, adLockReadOnly
                                  Set rng = .Range("H24")
                                  rng.CopyFromRecordset objRs6
                                  objRs6.Close
                         End With
    
                         With WS
                                  objRs5.Open sSQL5, CurrentProject.Connection, adOpenDynamic, adLockReadOnly
                                  Set rng = .Range("B24")
                                  rng.CopyFromRecordset objRs5
                                  objRs5.Close
                         End With
                    End With
                    With WB
                         Set WS = WB.Worksheets("Parámetros")
                         With WS
                                  objRs4.Open sSQL4, CurrentProject.Connection, adOpenDynamic, adLockReadOnly
                                  Set rng = .Range("B10")
                                  rng.CopyFromRecordset objRs4
                                  objRs4.Close
                         End With
                    End With
                    With WB
                         Set WS = WB.Worksheets("Inicio")
                         With WS
                            oExcel.Run "Bienvenidos"
                         End With
                    End With
    
    End With
    
    Set objRs = Nothing
    Set objRs2 = Nothing
    Set objRs3 = Nothing
    Set objRs4 = Nothing
    Set objRs5 = Nothing
    Set objRs6 = Nothing
    Set objRs7 = Nothing
    
    MsgBox ("!El informe se ha realizado correctamente!"), vbOKOnly
    
    End Sub
    
    BookOpen时的

    MyCode:

    Private Sub Workbook_Open()
      Application.WindowState = xlMaximized
    End Sub
    

    以我的形式:

    Private Sub UserForm_QueryClose _
    (Cancel As Integer, CloseMode As Integer)
    If CloseMode = vbFormControlMenu Then
            Cancel = True
    End If
    End Sub
    
    Private Sub UserForm_Activate()
        Call ActualizarCeldas
        Unload Me
    End Sub
    

1 个答案:

答案 0 :(得分:1)

如果要在Excel中显示用户表单但保持其他代码运行,则需要以非模态样式显示表单。

userFormName.Show False

如果您的userform有按钮或其他控件供用户单击,请务必小心,因为如果他们单击,则运行的代码可能会干扰/中断其他代码。

我无法在现有代码中看到当前显示的表单。