Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frmRelConsumoAlimentador
	Inherits System.Windows.Forms.Form
	Private istrCamposLinhas As String
	Private istrCamposColunas As String
	Private istrCamposSoma As String
	Private istrCriterio As String
	Private iintDivMWh As Short
	
	Private istrCabecalhoA As String
	Private istrCabecalhoB As String
	Private istrRodapeA As String
	Private istrRodapeB As String
	Private ifrmRelAlimentadores As FrmRelatorio
	Private istrTamCol As String
	
	Private Sub MontaCriterio()
		Dim llstrCri As Object
		Dim lstrCri As String
		lstrCri = ""
		'Referência
		If Me.opbFilPorPeriodo.Checked Then
			lstrCri = lstrCri & "(`nfv`.`mesano_referencia` BETWEEN " & VB6.Format(Me.dtpMesAnoIni.value, "yyyyMM") & " AND " & VB6.Format(Me.dtpMesAnoFim.value, "yyyyMM") & ") "
			istrCabecalhoB = "Período de " & VB6.Format(Me.dtpMesAnoIni.value, "MM/yyyy") & " até " & VB6.Format(Me.dtpMesAnoFim.value, "MM/yyyy")
		ElseIf Me.opbFilPorAno.Checked Then 
			lstrCri = lstrCri & "(`nfv`.`mesano_referencia` BETWEEN " & VB6.Format(Me.dtpAno.value, "yyyy") & "01" & " AND " & VB6.Format(Me.dtpAno.value, "yyyy") & "12 ) "
			istrCabecalhoB = "Referente ao ano de " & VB6.Format(Me.dtpAno.value, "yyyy")
		End If
		'Grupo A ou B
		If Me.opbFilGrupoA.Checked Then
			lstrCri = lstrCri & IIf(Trim(lstrCri) <> "", " AND ", "") & "`nfv`.`grupo` = 'A' "
			istrRodapeA = istrRodapeA & IIf(Trim(istrRodapeA) <> "", ",", "") & "Grupo A"
		ElseIf Me.opbFilGrupoB.Checked Then 
			lstrCri = lstrCri & IIf(Trim(lstrCri) <> "", " AND ", "") & "`nfv`.`grupo` = 'B' "
			istrRodapeA = istrRodapeA & IIf(Trim(istrRodapeA) <> "", ",", "") & "Grupo B"
		End If
		'Urbano ou não urbano
		If Me.opbFilRegiaoUrbano.Checked Then
			lstrCri = lstrCri & IIf(Trim(lstrCri) <> "", " AND ", "") & "`nfv`.`regiao` = 'U' "
			istrRodapeA = istrRodapeA & IIf(Trim(istrRodapeA) <> "", ",", "") & "Região Urbana"
		ElseIf Me.opbFilRegiaoRural.Checked Then 
			lstrCri = lstrCri & IIf(Trim(lstrCri) <> "", " AND ", "") & "`nfv`.`regiao` = 'R' "
			istrRodapeA = istrRodapeA & IIf(Trim(istrRodapeA) <> "", ",", "") & "Região Não Urbana"
		End If
		'Faturas em aberto ou pagas
		If Me.opbFilFaturasEmAberto.Checked Then
			lstrCri = lstrCri & IIf(Trim(lstrCri) <> "", " AND ", "") & "`nfv`.`flgst` = '' "
			istrRodapeA = istrRodapeA & IIf(Trim(istrRodapeA) <> "", ",", "") & "Faturas em Aberto"
		ElseIf Me.opbFilFaturasPagas.Checked Then 
			lstrCri = lstrCri & IIf(Trim(lstrCri) <> "", " AND ", "") & "`nfv`.`flgst` = 'P' "
			istrRodapeA = istrRodapeA & IIf(Trim(istrRodapeA) <> "", ",", "") & "Faturas Pagas"
		End If
		'Classes
		If Me.opbFilClasseResidencial.Checked Then
			lstrCri = lstrCri & IIf(Trim(lstrCri) <> "", " AND ", "") & "(`nfv`.`classe_faturada` BETWEEN '9100' AND '9199') "
			istrRodapeA = istrRodapeA & IIf(Trim(istrRodapeA) <> "", ",", "") & "Classe Residencial"
		ElseIf Me.opbFilClasseIndustrial.Checked Then 
			lstrCri = lstrCri & IIf(Trim(lstrCri) <> "", " AND ", "") & "(`nfv`.`classe_faturada` BETWEEN '0000' AND '3999') "
			istrRodapeA = istrRodapeA & IIf(Trim(istrRodapeA) <> "", ",", "") & "Classe Industrial"
		ElseIf Me.opbFilClasseComercial.Checked Then 
			'UPGRADE_WARNING: Couldn't resolve default property of object llstrCri. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			llstrCri = lstrCri & IIf(Trim(lstrCri) <> "", " AND ", "") & "(`nfv`.`classe_faturada` BETWEEN '4000' AND '4999') "
			istrRodapeA = istrRodapeA & IIf(Trim(istrRodapeA) <> "", ",", "") & "Classe Comercial"
		ElseIf Me.opbFilClasseRural.Checked Then 
			lstrCri = lstrCri & IIf(Trim(lstrCri) <> "", " AND ", "") & "(`nfv`.`classe_faturada` BETWEEN '5000' AND '5999') "
			istrRodapeA = istrRodapeA & IIf(Trim(istrRodapeA) <> "", ",", "") & "Classe Rural"
		ElseIf Me.opbFilClassePoderPublicoMunicipal.Checked Then 
			lstrCri = lstrCri & IIf(Trim(lstrCri) <> "", " AND ", "") & "(`nfv`.`classe_faturada` BETWEEN '6100' AND '6199') "
			istrRodapeA = istrRodapeA & IIf(Trim(istrRodapeA) <> "", ",", "") & "Classe Poder Público Municipal"
		ElseIf Me.opbFilClassePoderPublicoEstadual.Checked Then 
			lstrCri = lstrCri & IIf(Trim(lstrCri) <> "", " AND ", "") & "(`nfv`.`classe_faturada` BETWEEN '6200' AND '6299') "
			istrRodapeA = istrRodapeA & IIf(Trim(istrRodapeA) <> "", ",", "") & "Classe Poder Público Estadual"
		ElseIf Me.opbFilClassePoderPublicoFederal.Checked Then 
			lstrCri = lstrCri & IIf(Trim(lstrCri) <> "", " AND ", "") & "(`nfv`.`classe_faturada` BETWEEN '6300' AND '6399') "
			istrRodapeA = istrRodapeA & IIf(Trim(istrRodapeA) <> "", ",", "") & "Classe Poder Público Federal"
		ElseIf Me.opbFilClasseIluminacaoPublica.Checked Then 
			lstrCri = lstrCri & IIf(Trim(lstrCri) <> "", " AND ", "") & "(`nfv`.`classe_faturada` BETWEEN '6400' AND '6499') "
			istrRodapeA = istrRodapeA & IIf(Trim(istrRodapeA) <> "", ",", "") & "Classe Iluminação Pública"
		ElseIf Me.opbFilClasseServicoPublico.Checked Then 
			lstrCri = lstrCri & IIf(Trim(lstrCri) <> "", " AND ", "") & "(`nfv`.`classe_faturada` BETWEEN '7000' AND '7999') "
			istrRodapeA = istrRodapeA & IIf(Trim(istrRodapeA) <> "", ",", "") & "Classe Serviço Público"
		ElseIf Me.opbFilClasseConsumoProprio.Checked Then 
			lstrCri = lstrCri & IIf(Trim(lstrCri) <> "", " AND ", "") & "(`nfv`.`classe_faturada` BETWEEN '8000' AND '8999') "
			istrRodapeA = istrRodapeA & IIf(Trim(istrRodapeA) <> "", ",", "") & "Classe Consumo Próprio"
		End If
		istrCriterio = lstrCri
	End Sub
	
	Private Sub MontaColunas()
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		If Me.opbAgrGrupo.Checked Then
			istrCamposColunas = "`grupo` 'Grupo'"
			istrCabecalhoA = " por Grupo"
		ElseIf Me.opbAgrClasse.Checked Then 
			istrCamposColunas = "`descricao_classe` 'Classe'"
			istrCabecalhoA = " por Classe"
		ElseIf Me.opbAgrRegiao.Checked Then 
			istrCamposColunas = "IF(`regiao`='U','Urbano','Não Urbano') 'Região'"
			istrCabecalhoA = " por Região"
		ElseIf Me.opbAgrGrupoTarifario.Checked Then 
			istrCamposColunas = "`grupo_tarifario` 'Gr.Tar.'"
			istrCabecalhoA = " por Grupo Tarifário"
		ElseIf Me.opbAgrPorAno.Checked Then 
			istrCamposColunas = "LEFT(`mesano_referencia`,4) 'Ano'"
			istrCabecalhoA = " por Ano"
		ElseIf Me.opbAgrPorMes.Checked Then 
			istrCamposColunas = "RIGHT(`mesano_referencia`,2) 'Mês'"
			istrCabecalhoA = " por Mês"
		End If
		If istrCamposColunas <> "" Then
			'Colunas
			lstrSql = ""
			lstrSql = lstrSql & "SELECT DISTINCT " & istrCamposColunas
			lstrSql = lstrSql & "  FROM `nfv` "
			lstrSql = lstrSql & " WHERE " & istrCriterio
			lstrSql = lstrSql & " ORDER BY 1 ASC "
			Call Conectar(lstrSql, lrstAux)
			With lrstAux
				If .State = ADODB.ObjectStateEnum.adStateOpen Then
					If .RecordCount > 0 Then
						With Me.mshColunas
							Call .Clear()
							.Rows = 2
							.set_Cols( , 2)
							.AllowUserResizing = MSFlexGridLib.AllowUserResizeSettings.flexResizeBoth
							.AllowBigSelection = True
							.DataSource = lrstAux.DataSource
						End With
					End If
					Call .Close()
				End If
			End With
		End If
	End Sub
	
	Private Sub MontaLinhas()
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		
		istrCamposLinhas = "`id_alimentador` 'Cód.', IF(TRIM(`ds_alimentador`)<>'',`ds_alimentador`,'NE') 'Alimentador'"
		'Linhas
		lstrSql = ""
		lstrSql = lstrSql & "SELECT DISTINCT " & istrCamposLinhas
		lstrSql = lstrSql & "  FROM `nfv` "
		lstrSql = lstrSql & " WHERE " & istrCriterio
		lstrSql = lstrSql & " ORDER BY 1 ASC "
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .RecordCount > 0 Then
					With Me.mshLinhas
						Call .Clear()
						.Rows = 2
						.set_Cols( , 2)
						.AllowUserResizing = MSFlexGridLib.AllowUserResizeSettings.flexResizeBoth
						.AllowBigSelection = True
						.DataSource = lrstAux.DataSource
					End With
				End If
				Call .Close()
			End If
		End With
		
	End Sub
	
	Private Sub MontaSoma()
		If Me.opbSumQuantidadeConsumidores.Checked Then
			istrCamposSoma = "COUNT(*) 'Qtd.'"
			istrRodapeB = istrRodapeB & IIf(Trim(istrRodapeB) <> "", ",", "") & "Totalizado por Quantidade de Consumidores"
		Else
			If Me.opbSumTipoQuantidade.Checked Then
				If Me.opbSumConsumoEncargoTotal.Checked Then
					istrCamposSoma = "(SUM(ROUND(kwh_" & IIf(Me.opbSumTipoMedido.Checked, "lido", "faturado") & ",0))+" & "SUM(ROUND(kwh_faturado_ponta,0))+" & "SUM(ROUND(kwh_faturado_fora_ponta,0))+" & "SUM(ROUND(encargo_kwh_faturado_ponta,0))+" & "SUM(ROUND(encargo_kwh_faturado_fora_ponta,0)) ) 'kWh' "
					istrRodapeB = istrRodapeB & IIf(Trim(istrRodapeB) <> "", ",", "") & "Totalizado por quantidade Consumo+Encargo(ponta e fora ponta) " & IIf(Me.opbSumTipoMedido.Checked, "lido", "faturado")
				ElseIf Me.opbSumConsumoEncargoPonta.Checked Then 
					istrCamposSoma = "(SUM(ROUND(kwh_faturado_ponta,0))+" & "SUM(ROUND(encargo_kwh_faturado_ponta,0)) ) 'kWh Ponta' "
					istrRodapeB = istrRodapeB & IIf(Trim(istrRodapeB) <> "", ",", "") & "Totalizado por quantidade Consumo+Encargo(ponta) " & IIf(Me.opbSumTipoMedido.Checked, "lido", "faturado")
				ElseIf Me.opbSumConsumoEncargoForaPonta.Checked Then 
					istrCamposSoma = "(SUM(ROUND(kwh_" & IIf(Me.opbSumTipoMedido.Checked, "lido", "faturado") & ",0))+" & "SUM(ROUND(kwh_faturado_fora_ponta,0))+" & "SUM(ROUND(encargo_kwh_faturado_fora_ponta,0)) ) 'kWh Fora Ponta' "
					istrRodapeB = istrRodapeB & IIf(Trim(istrRodapeB) <> "", ",", "") & "Totalizado por quantidade Consumo+Encargo(fora ponta) " & IIf(Me.opbSumTipoMedido.Checked, "lido", "faturado")
				ElseIf Me.opbSumConsumoTotal.Checked Then 
					istrCamposSoma = "(SUM(ROUND(kwh_" & IIf(Me.opbSumTipoMedido.Checked, "lido", "faturado") & ",0))+" & "SUM(ROUND(kwh_faturado_ponta,0))+" & "SUM(ROUND(kwh_faturado_fora_ponta,0)) ) 'kWh' "
					istrRodapeB = istrRodapeB & IIf(Trim(istrRodapeB) <> "", ",", "") & "Totalizado por quantidade Consumo(ponta e fora ponta) " & IIf(Me.opbSumTipoMedido.Checked, "lido", "faturado")
				ElseIf Me.opbSumConsumoPonta.Checked Then 
					istrCamposSoma = "(SUM(ROUND(kwh_faturado_ponta,0)) ) 'kWh Ponta' "
					istrRodapeB = istrRodapeB & IIf(Trim(istrRodapeB) <> "", ",", "") & "Totalizado por quantidade Consumo(ponta) " & IIf(Me.opbSumTipoMedido.Checked, "lido", "faturado")
				ElseIf Me.opbSumConsumoForaPonta.Checked Then 
					istrCamposSoma = "(SUM(ROUND(kwh_" & IIf(Me.opbSumTipoMedido.Checked, "lido", "faturado") & ",0))+" & "SUM(ROUND(kwh_faturado_fora_ponta,0)) ) 'kWh Fora Ponta' "
					istrRodapeB = istrRodapeB & IIf(Trim(istrRodapeB) <> "", ",", "") & "Totalizado por quantidade Consumo(fora ponta) " & IIf(Me.opbSumTipoMedido.Checked, "lido", "faturado")
				ElseIf Me.opbSumEncargoTotal.Checked Then 
					istrCamposSoma = "(SUM(ROUND(encargo_kwh_faturado_ponta,0))+" & "SUM(ROUND(encargo_kwh_faturado_fora_ponta,0)) ) 'Encargo' "
					istrRodapeB = istrRodapeB & IIf(Trim(istrRodapeB) <> "", ",", "") & "Totalizado por quantidade Encargo(ponta e fora ponta) " & IIf(Me.opbSumTipoMedido.Checked, "lido", "faturado")
				ElseIf Me.opbSumEncargoPonta.Checked Then 
					istrCamposSoma = "(SUM(ROUND(encargo_kwh_faturado_ponta,0)) ) 'Enc.Ponta' "
					istrRodapeB = istrRodapeB & IIf(Trim(istrRodapeB) <> "", ",", "") & "Totalizado por quantidade Encargo(ponta) " & IIf(Me.opbSumTipoMedido.Checked, "lido", "faturado")
				ElseIf Me.opbSumEncargoForaPonta.Checked Then 
					istrCamposSoma = "(SUM(ROUND(encargo_kwh_faturado_fora_ponta,0)) ) 'Enc.Fora Ponta' "
					istrRodapeB = istrRodapeB & IIf(Trim(istrRodapeB) <> "", ",", "") & "Totalizado por quantidade Encargo(fora ponta) " & IIf(Me.opbSumTipoMedido.Checked, "lido", "faturado")
				ElseIf Me.opbSumDemandaTotal.Checked Then 
					istrCamposSoma = "(SUM(ROUND(kw_" & IIf(Me.opbSumTipoMedido.Checked, "lido", "faturado") & ",0))+" & "SUM(ROUND(kw_" & IIf(Me.opbSumTipoMedido.Checked, "lido", "faturado") & "_ponta,0))+" & "SUM(ROUND(kw_" & IIf(Me.opbSumTipoMedido.Checked, "lido", "faturado") & "_fora_ponta,0)) ) 'kW' "
					istrRodapeB = istrRodapeB & IIf(Trim(istrRodapeB) <> "", ",", "") & "Totalizado por quantidade Demanda(ponta e fora ponta) " & IIf(Me.opbSumTipoMedido.Checked, "lido", "faturado")
				ElseIf Me.opbSumDemandaPonta.Checked Then 
					istrCamposSoma = "(SUM(ROUND(kw_" & IIf(Me.opbSumTipoMedido.Checked, "lido", "faturado") & "_ponta,0)) ) 'kW Ponta' "
					istrRodapeB = istrRodapeB & IIf(Trim(istrRodapeB) <> "", ",", "") & "Totalizado por quantidade Demanda(ponta) " & IIf(Me.opbSumTipoMedido.Checked, "lido", "faturado")
				ElseIf Me.opbSumDemandaForaPonta.Checked Then 
					istrCamposSoma = "(SUM(ROUND(kw_" & IIf(Me.opbSumTipoMedido.Checked, "lido", "faturado") & ",0))+" & "SUM(ROUND(kw_" & IIf(Me.opbSumTipoMedido.Checked, "lido", "faturado") & "_fora_ponta,0)) ) 'kW Fora Ponta' "
					istrRodapeB = istrRodapeB & IIf(Trim(istrRodapeB) <> "", ",", "") & "Totalizado por quantidade Demanda(fora ponta) " & IIf(Me.opbSumTipoMedido.Checked, "lido", "faturado")
				End If
			ElseIf Me.opbSumTipoValor.Checked Then 
				If Me.opbSumConsumoEncargoTotal.Checked Then
					istrCamposSoma = "(ROUND(SUM(vlr_kwh_faturado),2)+" & "ROUND(SUM(valor_kwh_faturado_ponta),2)+" & "ROUND(SUM(valor_kwh_faturado_fora_ponta),2)+" & "ROUND(SUM(encargo_valor_kwh_faturado_ponta),2)+" & "ROUND(SUM(encargo_valor_kwh_faturado_fora_ponta),2) ) 'kWh' "
					istrRodapeB = istrRodapeB & IIf(Trim(istrRodapeB) <> "", ",", "") & "Totalizado por valor Consumo+Encargo(ponta e fora ponta)"
				ElseIf Me.opbSumConsumoEncargoPonta.Checked Then 
					istrCamposSoma = "(ROUND(SUM(valor_kwh_faturado_ponta),2)+" & "ROUND(SUM(encargo_valor_kwh_faturado_ponta),2) ) 'kWh Ponta' "
					istrRodapeB = istrRodapeB & IIf(Trim(istrRodapeB) <> "", ",", "") & "Totalizado por valor Consumo+Encargo(ponta)"
				ElseIf Me.opbSumConsumoEncargoForaPonta.Checked Then 
					istrCamposSoma = "(ROUND(SUM(vlr_kwh_faturado),2)+" & "ROUND(SUM(valor_kwh_faturado_fora_ponta),2)+" & "ROUND(SUM(encargo_valor_kwh_faturado_fora_ponta),2) ) 'kWh Fora Ponta' "
					istrRodapeB = istrRodapeB & IIf(Trim(istrRodapeB) <> "", ",", "") & "Totalizado por valor Consumo+Encargo(fora ponta)"
				ElseIf Me.opbSumConsumoTotal.Checked Then 
					istrCamposSoma = "(ROUND(SUM(vlr_kwh_faturado),2)+" & "ROUND(SUM(valor_kwh_faturado_ponta),2)+" & "ROUND(SUM(valor_kwh_faturado_fora_ponta),2) ) 'kWh' "
					istrRodapeB = istrRodapeB & IIf(Trim(istrRodapeB) <> "", ",", "") & "Totalizado por valor Consumo(ponta e fora ponta)"
				ElseIf Me.opbSumConsumoPonta.Checked Then 
					istrCamposSoma = "(ROUND(SUM(valor_kwh_faturado_ponta),2) ) 'kWh Ponta' "
					istrRodapeB = istrRodapeB & IIf(Trim(istrRodapeB) <> "", ",", "") & "Totalizado por valor Consumo(ponta)"
				ElseIf Me.opbSumConsumoForaPonta.Checked Then 
					istrCamposSoma = "(ROUND(SUM(vlr_kwh_faturado),2)+" & "ROUND(SUM(valor_kwh_faturado_fora_ponta),2) ) 'kWh Fora Ponta' "
					istrRodapeB = istrRodapeB & IIf(Trim(istrRodapeB) <> "", ",", "") & "Totalizado por valor Consumo(fora ponta)"
				ElseIf Me.opbSumEncargoTotal.Checked Then 
					istrCamposSoma = "(ROUND(SUM(encargo_valor_kwh_faturado_ponta),2)+" & "ROUND(SUM(encargo_valor_kwh_faturado_fora_ponta),2) ) 'Encargo' "
					istrRodapeB = istrRodapeB & IIf(Trim(istrRodapeB) <> "", ",", "") & "Totalizado por valor Encargo(ponta e fora ponta)"
				ElseIf Me.opbSumEncargoPonta.Checked Then 
					istrCamposSoma = "(ROUND(SUM(encargo_valor_kwh_faturado_ponta),2) ) 'Enc.Ponta' "
					istrRodapeB = istrRodapeB & IIf(Trim(istrRodapeB) <> "", ",", "") & "Totalizado por valor Encargo(ponta)"
				ElseIf Me.opbSumEncargoForaPonta.Checked Then 
					istrCamposSoma = "(ROUND(SUM(encargo_valor_kwh_faturado_fora_ponta),2) ) 'Enc.Fora Ponta' "
					istrRodapeB = istrRodapeB & IIf(Trim(istrRodapeB) <> "", ",", "") & "Totalizado por valor Encargo(fora ponta)"
				ElseIf Me.opbSumDemandaTotal.Checked Then 
					istrCamposSoma = "(ROUND(SUM(valor_kw_faturado),2)+" & "ROUND(SUM(valor_kw_faturado_ponta),2)+" & "ROUND(SUM(valor_kw_faturado_fora_ponta),2) ) 'kW' "
					istrRodapeB = istrRodapeB & IIf(Trim(istrRodapeB) <> "", ",", "") & "Totalizado por valor Demanda(ponta e fora ponta)"
				ElseIf Me.opbSumDemandaPonta.Checked Then 
					istrCamposSoma = "(ROUND(SUM(valor_kw_faturado_ponta),2) ) 'kW Ponta' "
					istrRodapeB = istrRodapeB & IIf(Trim(istrRodapeB) <> "", ",", "") & "Totalizado por valor Demanda(ponta)"
				ElseIf Me.opbSumDemandaForaPonta.Checked Then 
					istrCamposSoma = "(ROUND(SUM(valor_kw_faturado),2)+" & "ROUND(SUM(valor_kw_faturado_fora_ponta),2) ) 'kW Fora Ponta' "
					istrRodapeB = istrRodapeB & IIf(Trim(istrRodapeB) <> "", ",", "") & "Totalizado por valor Demanda(fora ponta)"
				End If
			End If
		End If
	End Sub
	
	'UPGRADE_WARNING: Event chkEmMegaWattHora.CheckStateChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub chkEmMegaWattHora_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkEmMegaWattHora.CheckStateChanged
		iintDivMWh = 1
		If Not Me.opbSumQuantidadeConsumidores.Checked And Not Me.opbSumDemandaTotal.Checked And Not Me.opbSumDemandaPonta.Checked And Not Me.opbSumDemandaForaPonta.Checked Then
			If Me.chkEmMegaWattHora.CheckState = System.Windows.Forms.CheckState.Checked Then
				iintDivMWh = 1000
			End If
		End If
	End Sub
	
	Private Sub cmdArquivo_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdArquivo.Click
		Me.dialArqOpen.DefaultExt = "CSV"
		'UPGRADE_WARNING: Filter has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		Me.dialArqOpen.Filter = "*." & Me.dialArqOpen.DefaultExt
		Me.dialArqOpen.FileName = My.Application.Info.DirectoryPath & "\DadosPorAlimentador." & Me.dialArqOpen.DefaultExt
		Me.dialArqOpen.Title = "Arquivo de exportação em formato CSV"
		Me.dialArqOpen.ShowDialog()
		Me.txtArquivo.Text = Me.dialArqOpen.FileName
	End Sub
	
	Private Sub cmdExporta_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExporta.Click
		Call LigDes(False)
		Me.lblStatus.Text = "AGUARDE A EXPORTAÇÃO"
		System.Windows.Forms.Application.DoEvents()
		If Me.opbExpTotalizada.Checked Then
			Call ExportaTotalizado()
		Else
			Call ExportaDetalhado()
		End If
		Me.lblStatus.Text = "FINALIZADO"
		Call LigDes(True)
		System.Windows.Forms.Application.DoEvents()
	End Sub
	
	Private Sub cmdFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFechar.Click
		Call Me.Close()
	End Sub
	
	Private Sub cmdImprimir_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdImprimir.Click
		Call LigDes(False)
		Me.lblStatus.Text = "AGUARDE A IMPRESSÃO"
		Me.lblStatus.Refresh()
		System.Windows.Forms.Application.DoEvents()
		Call Imprimir()
		Me.lblStatus.Text = "FINALIZADO"
		Me.lblStatus.Refresh()
		Call LigDes(True)
		System.Windows.Forms.Application.DoEvents()
	End Sub
	
	Private Sub Imprimir()
		Dim lstrTab As String
		Dim llngRow As Integer
		Dim llngCol As Integer
		istrTamCol = "<20mm|^25mm|>50mm|>50mm|>0mm;|"
		With ifrmRelAlimentadores
			.quem_chama = Me
			.VS.Font = VB6.FontChangeName(.VS.Font, "Arial")
			.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
			.VS.PageBorder = VSVIEW6.TableBorderSettings.tbNone
			.VS.Orientation = VSVIEW6.OrientationSettings.orLandscape
			.VS.PhysicalPage = True
			.VS.PaperSize = VSVIEW6.PaperSizeSettings.pprA4
			.VS.MarginTop = "7mm"
			.VS.MarginRight = "7mm"
			.VS.MarginLeft = "7mm"
			.VS.MarginBottom = "5mm"
			.VS.TextColor = System.Drawing.Color.Black
			.VS.BrushStyle = VSVIEW6.BrushStyleSettings.bsSolid
			.VS.Font = VB6.FontChangeBold(.VS.Font, False)
			.VS.StartDoc()
			.VS.CurrentLine = 0
			.VS.Font = VB6.FontChangeSize(.VS.Font, 9)
			Call Cabecalho_Relatorio(.VS, "Relatório de Alimentador " & istrCabecalhoA, istrCabecalhoB, "3")
			.VS.Font = VB6.FontChangeBold(.VS.Font, True)
			Call LineFeed(.VS, 2)
			.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
			'Cabeçalho dos dados
			With Me.mshDados
				lstrTab = ""
				For llngCol = 1 To .get_Cols() - 1
					lstrTab = lstrTab & .get_TextMatrix(0, llngCol) & IIf(llngCol < .get_Cols() - 1, "|", "")
				Next llngCol
			End With
			.VS.Table = istrTamCol & lstrTab
			.VS.Font = VB6.FontChangeBold(.VS.Font, False)
		End With
		'Dados
		With Me.mshDados
			For llngRow = 1 To .Rows - 1
				lstrTab = ""
				For llngCol = 1 To .get_Cols() - 1
					lstrTab = lstrTab & .get_TextMatrix(llngRow, llngCol) & IIf(llngCol < .get_Cols() - 1, "|", "")
				Next llngCol
				ifrmRelAlimentadores.VS.Table = istrTamCol & lstrTab
			Next llngRow
		End With
		With ifrmRelAlimentadores
			.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
			.VS.Font = VB6.FontChangeBold(.VS.Font, True)
			If Trim(istrRodapeA) <> "" Then
				Call LineFeed(.VS, 1)
				.VS.Table = "<185mm;" & istrRodapeA
			End If
			If Trim(istrRodapeB) <> "" Then
				Call LineFeed(.VS, 1)
				.VS.Table = "<185mm;" & istrRodapeB
			End If
			.VS.Font = VB6.FontChangeBold(.VS.Font, False)
			'Gráfico
			Call LineFeed(.VS, 1)
			Call My.Computer.Clipboard.Clear()
			Call Me.mschConsumo.EditCopy()
			'UPGRADE_ISSUE: VBControlExtender method ifrmRelAlimentadores.VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			Call .VS.DrawPicture(My.Computer.Clipboard.GetImage(), "5mm", .VS.CurrentY + 80, 15000, 5000)
			'
			Call .VS.EndDoc()
			Call VB6.ShowForm(ifrmRelAlimentadores, False)
		End With
	End Sub
	Private Sub cmdProcessar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdProcessar.Click
		Dim lintAux As Short
		istrCabecalhoA = ""
		istrCabecalhoB = ""
		istrRodapeA = ""
		istrRodapeB = ""
		Call LigDes(False)
		Me.lblStatus.Text = "AGUARDE TOTALIZANDO"
		Call Me.lblStatus.Refresh()
		System.Windows.Forms.Application.DoEvents()
		With Me.mschConsumo
			.Repaint = True
			.ColumnCount = 0
			.RowCount = 0
			.RowCount = 0
			.ColumnLabelCount = 0
		End With
		Call MontaCriterio()
		Call MontaColunas()
		Call MontaLinhas()
		Call MontaSoma()
		Call Processar()
		Call MontaGrafico()
		Me.lblStatus.Text = "FINALIZADO"
		Call Me.lblStatus.Refresh()
		Call LigDes(True)
		Me.sstOpcoes.SelectedIndex = 1
		System.Windows.Forms.Application.DoEvents()
	End Sub
	
	Private Sub cmdTipo2dPizza_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdTipo2dPizza.Click
		Me.mschConsumo.chartType = MSChart20Lib.VtChChartType.VtChChartType2dPie
	End Sub
	
	Private Sub cmdTipo2dBarra_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdTipo2dBarra.Click
		Me.mschConsumo.chartType = MSChart20Lib.VtChChartType.VtChChartType2dBar
	End Sub
	
	Private Sub cmdTipo2dLinha_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdTipo2dLinha.Click
		Me.mschConsumo.chartType = MSChart20Lib.VtChChartType.VtChChartType2dLine
	End Sub
	
	Private Sub cmdTrocaXY_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdTrocaXY.Click
		If Me.mschConsumo.Plot.DataSeriesInRow Then
			Me.mschConsumo.Plot.DataSeriesInRow = False
		Else
			Me.mschConsumo.Plot.DataSeriesInRow = True
		End If
	End Sub
	
	
	Private Sub frmRelConsumoAlimentador_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		If KeyAscii = System.Windows.Forms.Keys.Escape Then
			Call cmdFechar_Click(cmdFechar, New System.EventArgs())
		ElseIf KeyAscii = System.Windows.Forms.Keys.Return Then 
			Call SendKeysA(System.Windows.Forms.Keys.Tab, True)
		End If
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Sub frmRelConsumoAlimentador_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Call CenterForm(Me)
		ifrmRelAlimentadores = New FrmRelatorio
		iintDivMWh = 1
	End Sub
	
	Private Sub Processar()
		Dim llngCol As Integer
		Dim llngRow As Integer
		Dim lstrFormato As String
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		
		If Me.opbSumTipoValor.Checked Then
			lstrFormato = "#,##0.00"
		ElseIf Me.opbSumTipoQuantidade.Checked Then 
			lstrFormato = "#0"
		End If
		lstrSql = ""
		lstrSql = lstrSql & "SELECT " & istrCamposLinhas & ", " & istrCamposColunas & "," & istrCamposSoma
		lstrSql = lstrSql & "  FROM `nfv` "
		lstrSql = lstrSql & " WHERE " & istrCriterio
		lstrSql = lstrSql & " GROUP BY 1, 3 "
		lstrSql = lstrSql & " ORDER BY 1 ASC, 3 ASC "
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .RecordCount > 0 Then
					With Me.mshDados
						Call .Clear()
						'          .Rows = 2
						'          .Cols = 3
						.AllowUserResizing = MSFlexGridLib.AllowUserResizeSettings.flexResizeBoth
						.AllowBigSelection = True
						'          Set .DataSource = lrstAux.DataSource
					End With
					Me.mshDados.Rows = .RecordCount + 1
					Me.mshDados.set_Cols( , .Fields.Count)
					For llngCol = 0 To .Fields.Count - 1
						Me.mshDados.set_TextMatrix(0, llngCol, .Fields(llngCol).Name)
					Next llngCol
					llngRow = 1
					While Not .EOF
						For llngCol = 0 To .Fields.Count - 1
							If llngCol = 3 Then
								'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
								Me.mshDados.set_TextMatrix(llngRow, llngCol, VB6.Format(IIf(IsDbNull(.Fields(llngCol).value), 0, .Fields(llngCol).value), lstrFormato))
							Else
								'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
								Me.mshDados.set_TextMatrix(llngRow, llngCol, IIf(IsDbNull(.Fields(llngCol).value), "", .Fields(llngCol).value))
							End If
						Next 
						Call .MoveNext()
						llngRow = llngRow + 1
					End While
				End If
				Call .Close()
			End If
		End With
	End Sub
	
	Private Sub GraficoMaiorValor()
		Dim ldblMax As Object
		Dim lintDad As Short
		For lintDad = 1 To Me.mshDados.Rows - 1
			If Me.chkEmMegaWattHora.CheckState = System.Windows.Forms.CheckState.Checked Then
				'UPGRADE_WARNING: Couldn't resolve default property of object ldblMax. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
				If ldblMax < CDbl(Me.mshDados.get_TextMatrix(lintDad, 3)) / iintDivMWh Then
					'UPGRADE_WARNING: Couldn't resolve default property of object ldblMax. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
					ldblMax = CDbl(Me.mshDados.get_TextMatrix(lintDad, 3)) / iintDivMWh
				End If
			Else
				'UPGRADE_WARNING: Couldn't resolve default property of object ldblMax. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
				If ldblMax < CDbl(Me.mshDados.get_TextMatrix(lintDad, 3)) Then
					'UPGRADE_WARNING: Couldn't resolve default property of object ldblMax. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
					ldblMax = CDbl(Me.mshDados.get_TextMatrix(lintDad, 3))
				End If
			End If
		Next lintDad
		With Me.mschConsumo
			.Visible = False
			.ColumnCount = Me.mshColunas.Rows - 1
			.ColumnLabelCount = .ColumnCount
			.RowCount = Me.mshLinhas.Rows - 1
			.RowLabelCount = .RowCount
			.Plot.Axis(MSChart20Lib.VtChAxisId.VtChAxisIdY).ValueScale.Auto = False
			'UPGRADE_WARNING: Couldn't resolve default property of object ldblMax. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			.Plot.Axis(MSChart20Lib.VtChAxisId.VtChAxisIdY).ValueScale.Maximum = ldblMax
			.Plot.Axis(MSChart20Lib.VtChAxisId.VtChAxisIdY).ValueScale.Minimum = 0
			.Plot.Axis(MSChart20Lib.VtChAxisId.VtChAxisIdY).ValueScale.MajorDivision = 0
			.Plot.Axis(MSChart20Lib.VtChAxisId.VtChAxisIdY).ValueScale.MinorDivision = 1
			.Plot.Axis(MSChart20Lib.VtChAxisId.VtChAxisIdY).AxisScale.Hide = True
			.Plot.Axis(MSChart20Lib.VtChAxisId.VtChAxisIdY).AxisScale.PercentBasis = MSChart20Lib.VtChPercentAxisBasis.VtChPercentAxisBasisMaxRow
			.Plot.Axis(MSChart20Lib.VtChAxisId.VtChAxisIdY).AxisScale.Type = MSChart20Lib.VtChScaleType.VtChScaleTypeLinear
			.Plot.Axis(MSChart20Lib.VtChAxisId.VtChAxisIdY).CategoryScale.Auto = False
		End With
	End Sub
	
	Private Sub MontaGrafico()
		Dim ldblMax As Double
		Dim lintRow As Short
		Dim lintCol As Short
		Dim lintDad As Short
		Call GraficoMaiorValor()
		With Me.mschConsumo
			For lintCol = 1 To Me.mshColunas.Rows - 1
				.Column = lintCol
				.ColumnLabel = Me.mshColunas.get_TextMatrix(lintCol, 0) 'grupo
				For lintRow = 1 To Me.mshLinhas.Rows - 1
					.Row = lintRow
					.RowLabel = Me.mshLinhas.get_TextMatrix(lintRow, 1) 'alimentador
					
					'Dados
					For lintDad = 1 To Me.mshDados.Rows - 1
						If .ColumnLabel = Me.mshDados.get_TextMatrix(lintDad, 2) And .RowLabel = Me.mshDados.get_TextMatrix(lintDad, 1) Then
							If Me.chkEmMegaWattHora.CheckState = System.Windows.Forms.CheckState.Checked Then
								.data = CStr(IIf(IsNumeric(.data), .data, 0) + (CDbl(Me.mshDados.get_TextMatrix(lintDad, 3)) / iintDivMWh)) 'valor
							Else
								.data = CStr(IIf(IsNumeric(.data), .data, 0) + (Me.mshDados.get_TextMatrix(lintDad, 3))) 'valor
							End If
							lintDad = Me.mshDados.Rows - 1
						End If
					Next lintDad
				Next lintRow
			Next lintCol
		End With
		With Me.mschConsumo
			.Visible = True
		End With
	End Sub
	
	Private Sub frmRelConsumoAlimentador_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
		'UPGRADE_NOTE: Object ifrmRelAlimentadores may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
		ifrmRelAlimentadores = Nothing
	End Sub
	
	'UPGRADE_WARNING: Event opbFilPorAno.CheckedChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub opbFilPorAno_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles opbFilPorAno.CheckedChanged
		If eventSender.Checked Then
			Me.dtpAno.Enabled = Me.opbFilPorAno.Checked
		End If
	End Sub
	
	'UPGRADE_WARNING: Event opbFilPorPeriodo.CheckedChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub opbFilPorPeriodo_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles opbFilPorPeriodo.CheckedChanged
		If eventSender.Checked Then
			Me.dtpMesAnoIni.Enabled = Me.opbFilPorPeriodo.Checked
			Me.dtpMesAnoFim.Enabled = Me.opbFilPorPeriodo.Checked
		End If
	End Sub
	
	'UPGRADE_WARNING: Event opbSumTipoMedido.CheckedChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub opbSumTipoMedido_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles opbSumTipoMedido.CheckedChanged
		If eventSender.Checked Then
			If Me.opbSumTipoMedido.Checked Then
				Me.opbSumTipoQuantidade.Checked = True
			End If
		End If
	End Sub
	
	'UPGRADE_WARNING: Event opbSumTipoValor.CheckedChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub opbSumTipoValor_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles opbSumTipoValor.CheckedChanged
		If eventSender.Checked Then
			If Me.opbSumTipoValor.Checked Then
				Me.opbSumTipoFaturado.Checked = True
			End If
		End If
	End Sub
	
	Private Sub ExportaDetalhado()
		'Controle Arquivo
		Dim lstrLin As String
		Dim lintARQ As Short
		Dim llngRow As Integer
		Dim llngCol As Integer
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		If Trim(Me.txtArquivo.Text) = "" Then
			Call MsgBox("Você deve selecionar um local e o nome do arquivo que deseja gravar!" & vbCrLf & "Ex: C:\NomeArquivo.CSV", MsgBoxStyle.Critical, gstrTituloMensagem)
		Else
			lstrSql = "SELECT "
			lstrSql = lstrSql & "`ds_alimentador` 'Alimentador', `mesano_referencia` 'Ano Mês', `numero_fatura` 'Fatura',"
			lstrSql = lstrSql & "`zona` 'Zona', `livro` 'Livro', `conta` 'Conta', `nome` 'Nome',"
			lstrSql = lstrSql & "`grupo` 'Grupo',`descricao_classe` 'Classe',`grupo_tarifario` 'Grupo Tarifário',"
			lstrSql = lstrSql & "IF(`regiao`='R','Não Urbana','Urbana') 'Região',"
			lstrSql = lstrSql & "IF(`flgst`='P','Paga',IF(`flgst`='C','Cancelada','Em Aberta')) 'Situação',"
			lstrSql = lstrSql & "`data_leitura_atual` 'Data L.Atual', `data_leitura_anterior` 'Data L.Anterior',"
			lstrSql = lstrSql & "(`kwh_faturado_ponta`)                                 'Qtd Consumo Lido Ponta', "
			lstrSql = lstrSql & "(`kwh_lido` + `kwh_faturado_fora_ponta`)               'Qtd Consumo Lido Fora Ponta', "
			lstrSql = lstrSql & "(`kwh_faturado_ponta`)                                 'Qtd Consumo Faturado Ponta', "
			lstrSql = lstrSql & "(`kwh_faturado` + `kwh_faturado_fora_ponta`)           'Qtd Consumo Faturado Fora Ponta', "
			lstrSql = lstrSql & "(`encargo_kwh_faturado_ponta`)                         'Qtd Encargo Ponta', "
			lstrSql = lstrSql & "(`encargo_kwh_faturado_fora_ponta`)                    'Qtd Encargo Fora Ponta', "
			lstrSql = lstrSql & "(`kw_lido_ponta`)                                      'Qtd Demanda Lida Ponta', "
			lstrSql = lstrSql & "(`kw_lido` + `kw_lido_fora_ponta`)                     'Qtd Demanda Lida Fora Ponta', "
			lstrSql = lstrSql & "(`kw_faturado_ponta`)                                  'Qtd Demanda Faturada Ponta', "
			lstrSql = lstrSql & "(`kw_faturado` + `kw_faturado_fora_ponta`)             'Qtd Demanda Faturada Fora Ponta', "
			lstrSql = lstrSql & "(`valor_kwh_faturado_ponta`)                           'Vlr Consumo Faturado Ponta', "
			lstrSql = lstrSql & "(`vlr_kwh_faturado` + `valor_kwh_faturado_fora_ponta`) 'Vlr Consumo Faturado Fora Ponta', "
			lstrSql = lstrSql & "(`encargo_valor_kwh_faturado_ponta`)                   'Vlr Encargo Ponta', "
			lstrSql = lstrSql & "(`encargo_valor_kwh_faturado_fora_ponta`)              'Vlr Encargo Fora Ponta', "
			lstrSql = lstrSql & "(`valor_kw_faturado_ponta`)                            'Vlr Demanda Ponta', "
			lstrSql = lstrSql & "(`valor_kw_faturado` + `valor_kw_faturado_fora_ponta`) 'Vlr Demanda Fora Ponta' "
			lstrSql = lstrSql & "  FROM `nfv` "
			lstrSql = lstrSql & " WHERE " & istrCriterio
			Call Conectar(lstrSql, lrstAux)
			With lrstAux
				If .State = ADODB.ObjectStateEnum.adStateOpen Then
					If .RecordCount > 0 Then
						'Export para CSV
						lintARQ = FreeFile
						FileOpen(lintARQ, Me.txtArquivo.Text, OpenMode.Output)
						Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .RecordCount, " Gerando Arquivo ")
						lstrLin = ""
						For llngCol = 0 To .Fields.Count - 1
							lstrLin = lstrLin & .Fields(llngCol).Name & ";"
						Next 
						PrintLine(lintARQ, lstrLin)
						While Not .EOF
							lstrLin = ""
							For llngCol = 0 To .Fields.Count - 1
								If VB.Left(.Fields(llngCol).Name, 3) = "Qtd" Then
									'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
									lstrLin = lstrLin & VB6.Format(IIf(IsDbNull(.Fields(llngCol).value), 0, .Fields(llngCol).value), "#,#####0.00000") & ";"
								ElseIf VB.Left(.Fields(llngCol).Name, 3) = "Vlr" Then 
									'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
									lstrLin = lstrLin & VB6.Format(IIf(IsDbNull(.Fields(llngCol).value), 0, .Fields(llngCol).value), "#,##0.00") & ";"
								Else
									'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
									lstrLin = lstrLin & """" & IIf(IsDbNull(.Fields(llngCol).value), "", .Fields(llngCol).value) & """" & ";"
								End If
							Next 
							PrintLine(lintARQ, lstrLin)
							Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " Gerando Arquivo ")
							Call .MoveNext()
						End While
						FileClose(lintARQ)
					End If
					Call .Close()
				End If
			End With
			Call MsgBox("Arquivo gerado com sucesso!", MsgBoxStyle.Information, gstrTituloMensagem)
		End If
		'UPGRADE_NOTE: Object lrstAux may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
		lrstAux = Nothing
	End Sub
	
	Private Sub ExportaTotalizado()
		'Controle Arquivo
		Dim lstrLin As String
		Dim lintARQ As Short
		Dim llngRow As Integer
		Dim llngCol As Integer
		If Trim(Me.txtArquivo.Text) = "" Then
			Call MsgBox("Você deve selecionar um local e o nome do arquivo que deseja gravar!" & vbCrLf & "Ex: C:\NomeArquivo.CSV", MsgBoxStyle.Critical, gstrTituloMensagem)
		Else
			'Export para CSV
			If Trim(Me.txtArquivo.Text) <> "" Then
				lintARQ = FreeFile
				FileOpen(lintARQ, Me.txtArquivo.Text, OpenMode.Output)
				With Me.mshDados
					If .Rows > 1 Then
						Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .Rows - 1, " Gerando Arquivo ")
						For llngRow = 0 To .Rows - 1
							lstrLin = ""
							For llngCol = 0 To .get_Cols() - 1
								lstrLin = lstrLin & Replace(.get_TextMatrix(llngRow, llngCol), ";", ",") & ";"
							Next llngCol
							PrintLine(lintARQ, lstrLin)
							Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " Gerando Arquivo ")
						Next llngRow
					End If
				End With
				FileClose(lintARQ)
				Call MsgBox("Arquivo gerado com sucesso!", MsgBoxStyle.Information, gstrTituloMensagem)
			Else
				Call MsgBox("Você precisa selecionar o caminho e o arquivo para exportação!", MsgBoxStyle.Information, gstrTituloMensagem)
			End If
		End If
	End Sub
	
	Private Sub LigDes(ByVal ablnModo As Boolean)
		Me.cmdArquivo.Enabled = ablnModo
		Me.cmdExporta.Enabled = ablnModo
		Me.cmdFechar.Enabled = ablnModo
		Me.cmdImprimir.Enabled = ablnModo
		Me.cmdTipo2dBarra.Enabled = ablnModo
		Me.cmdTipo2dLinha.Enabled = ablnModo
		Me.cmdTipo2dPizza.Enabled = ablnModo
		Me.cmdTrocaXY.Enabled = ablnModo
		Me.sstOpcoes.Enabled = ablnModo
	End Sub
End Class