Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frmExportaSubvencoes
	Inherits System.Windows.Forms.Form
	Private ifrmRelAuxiliar As FrmRelatorio
	Private istrTamCol As String
	Private istrNmArq As String
	
	Private Sub cmdFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFechar.Click
		Call Executar("DROP TEMPORARY TABLE IF EXISTS tmp_exporta_subvencoes")
		Call Executar("DROP TEMPORARY TABLE IF EXISTS tmp_exporta_subvencoes_det")
		Call Me.Close()
	End Sub
	
	Private Sub cmdProcessar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdProcessar.Click
		Dim ldteLoop As Date
		Dim llngAnoMes As Integer
		'UPGRADE_WARNING: Couldn't resolve default property of object Me.dtpMesAno(1).value. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		'UPGRADE_WARNING: Couldn't resolve default property of object Me.dtpMesAno(0).value. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		If Me.dtpMesAno(0).value > Me.dtpMesAno(1).value Then
			Call MsgBox("O período final não pode ser menor que o período inicial!", MsgBoxStyle.Critical, gstrTituloMensagem)
		Else
			Me.dtpMesAno(0).value = CDate("01/" & VB6.Format(Me.dtpMesAno(0).value, "MM/yyyy"))
			Me.dtpMesAno(1).value = CDate(UltimoDiaMes(Me.dtpMesAno(1).Month, Me.dtpMesAno(1).Year) & "/" & VB6.Format(Me.dtpMesAno(1).value, "MM/yyyy"))
			Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
			Call CriaTabelas()
			Call LigDes(False)
			'UPGRADE_WARNING: Couldn't resolve default property of object Me.dtpMesAno().value. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			ldteLoop = Me.dtpMesAno(0).value
			While ldteLoop <= Me.dtpMesAno(1).value
				llngAnoMes = CInt(VB6.Format(ldteLoop, "yyyyMM"))
				Call Processar(llngAnoMes)
				ldteLoop = DateAdd(Microsoft.VisualBasic.DateInterval.Month, 1, ldteLoop)
			End While
			Call LigDes(True)
			Me.Cursor = System.Windows.Forms.Cursors.Default
			Call MsgBox("Processo concluído, escolha a forma de exportação!", MsgBoxStyle.Information, gstrTituloMensagem)
		End If
	End Sub
	
	Private Sub Processar(ByVal alngAnoMes As Integer)
		Dim lstrZona As String
		Dim lstrIdent As String
		Dim lstrCpfCnpj As String
		Dim lstrNomeC As String
		Dim lstrClass As String
		Dim llngClass As Integer
		Dim llngTipos As Integer
		Dim lstrDsCla As String
		Dim ldblNumNF As Double
		Dim lstrMesAn As String
		Dim llngMARef As Integer
		Dim ldteEmiss As Date
		Dim ldteApres As Date
		Dim ldteVecto As Date
		Dim ldteLeAtu As Date
		Dim ldteLeAnt As Date
		Dim lstrGrTar As String
		Dim lstrGrupo As String
		Dim lstrItens As String
		
		Dim ldblQtkWh As Double
		Dim ldblQkWhP As Double
		Dim ldblQkWhF As Double
		
		Dim ldblVlkWh As Double
		Dim ldblVkWhp As Double
		Dim ldblVkWhf As Double
		
		Dim ldblNVlkWh As Double
		Dim ldblNVkWhP As Double
		Dim ldblNVkWhF As Double
		
		Dim ldblVlrLddp As Double
		Dim ldblVlrLddr As Double
		Dim ldblVlrLddd As Double
		Dim ldblVlrLdcd As Double
		
		Dim ldblVlrLedp As Double
		Dim ldblVlrLedr As Double
		Dim ldblVlrLedd As Double
		Dim ldblVlrLecd As Double
		
		Dim lintMpr As Short
		
		Dim ldblTarFt As Double
		Dim ldblTarNr As Double
		Dim ldblTaNrP As Double
		Dim ldblTaNrF As Double
		
		Dim ldblVrDes As Double
		Dim ldblVrIrr As Double
		
		Dim lintIrrig As Short
		
		Dim lblnInsere As Boolean
		Dim ldteBase As Date
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		Me.lblInfo.Text = " Buscando os dados das faturas de " & VB.Right(CStr(alngAnoMes), 2) & "/" & VB.Left(CStr(alngAnoMes), 4) & ". Aguarde..."
		Call Me.lblInfo.Refresh()
		ldteBase = CDate(UltimoDiaMes(CShort(VB.Right(CStr(alngAnoMes), 2)), CShort(VB.Left(CStr(alngAnoMes), 4))) & "/" & VB.Right(CStr(alngAnoMes), 2) & "/" & VB.Left(CStr(alngAnoMes), 4))
		lintMpr = 1
		lstrSql = ""
		lstrSql = lstrSql & "SELECT `nf`.`zona`, "
		lstrSql = lstrSql & "       `nf`.`livro`, "
		lstrSql = lstrSql & "       `nf`.`conta`, "
		lstrSql = lstrSql & "       `nf`.`cons_id`, "
		lstrSql = lstrSql & "       `nf`.`unid_id`, "
		lstrSql = lstrSql & "       `nf`.`cpf_cnpj`, "
		lstrSql = lstrSql & "       IFNULL(IFNULL(`unida`.`nome`,`unidb`.`nome`),`consum`.`nome`) `nome_consumidor`, "
		lstrSql = lstrSql & "       `nf`.`ta`, "
		lstrSql = lstrSql & "       `nf`.`codclas`, "
		lstrSql = lstrSql & "       `classe`.`descricao` `descricao_classe`, "
		lstrSql = lstrSql & "       `nf`.`numnf`, "
		lstrSql = lstrSql & "       `nf`.`mesano`, "
		lstrSql = lstrSql & "       `nf`.`mesano_ref`, "
		lstrSql = lstrSql & "       `nf`.`dataem`, "
		lstrSql = lstrSql & "       `nf`.`dataapr`, "
		lstrSql = lstrSql & "       `nf`.`datavenc`, "
		lstrSql = lstrSql & "       `nf`.`dataleit`, "
		lstrSql = lstrSql & "       `nf`.`dataleitant`, "
		lstrSql = lstrSql & "       `nf`.`grupo`, "
		lstrSql = lstrSql & "       `nf`.`irrigacao_aquicultura`, "
		lstrSql = lstrSql & "       `nf`.`micro_geracao`, "
		lstrSql = lstrSql & "       `nf`.`parcialmente_cativo`, "
		lstrSql = lstrSql & "       `nf`.`livreespecial`, "
		lstrSql = lstrSql & "       `nf`.`itensserv` "
		lstrSql = lstrSql & "  FROM `nf` "
		lstrSql = lstrSql & " LEFT JOIN `unida` ON(`unida`.`cons_id` = `nf`.`cons_id` AND `unida`.`unid_id` = `nf`.`unid_id` AND `nf`.`grupo` = 'A') "
		lstrSql = lstrSql & " LEFT JOIN `unidb` ON(`unidb`.`cons_id` = `nf`.`cons_id` AND `unidb`.`unid_id` = `nf`.`unid_id` AND `nf`.`grupo` = 'B') "
		lstrSql = lstrSql & " LEFT JOIN `consum` ON(`consum`.`consreg` = `nf`.`cons_id`) "
		lstrSql = lstrSql & " LEFT JOIN `classe` ON(`classe`.`codigo` = `nf`.`codclas`) "
		lstrSql = lstrSql & " WHERE `nf`.`mesano_ref` = " & alngAnoMes & " "
		lstrSql = lstrSql & "   AND (`nf`.`flgst` <> 'C' OR (`nf`.`flgst` = 'C' AND `nf`.`datast` > '" & VB6.Format(ldteBase, "yyyy-MM-dd") & "')) "
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .RecordCount > 0 Then
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .RecordCount, " " & (VB.Right(CStr(alngAnoMes), 2) & "/" & VB.Left(CStr(alngAnoMes), 4)) & " ")
					While Not .EOF
						
						ldblQtkWh = 0
						ldblQkWhP = 0
						ldblQkWhF = 0
						
						ldblVlkWh = 0
						ldblVkWhp = 0
						ldblVkWhf = 0
						
						ldblNVlkWh = 0
						ldblNVkWhP = 0
						ldblNVkWhF = 0
						
						ldblVlrLddp = 0
						ldblVlrLddr = 0
						ldblVlrLddd = 0
						ldblVlrLdcd = 0
						
						ldblVlrLedp = 0
						ldblVlrLedr = 0
						ldblVlrLedd = 0
						ldblVlrLecd = 0
						
						ldblVrIrr = 0
						ldblTarNr = 0
						ldblTaNrP = 0
						ldblTaNrF = 0
						ldblVrDes = 0
						lblnInsere = False
						'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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						lstrZona = Trim(IIf(IsDbNull(.Fields("zona").value), "000", .Fields("zona").value))
						'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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						lstrIdent = Trim(IIf(IsDbNull(.Fields("zona").value), "000", .Fields("zona").value))
						'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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						lstrIdent = lstrIdent & Trim(IIf(IsDbNull(.Fields("livro").value), "00", .Fields("livro").value))
						'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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						lstrIdent = lstrIdent & Trim(IIf(IsDbNull(.Fields("conta").value), "000000", .Fields("conta").value))
						'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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						lstrCpfCnpj = Trim(IIf(IsDbNull(.Fields("cpf_cnpj").value), "", .Fields("cpf_cnpj").value))
						'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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						lstrNomeC = Trim(IIf(IsDbNull(.Fields("nome_consumidor").value), "sem nome", .Fields("nome_consumidor").value))
						'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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						lstrClass = Trim(IIf(IsDbNull(.Fields("codclas").value), "0000", .Fields("codclas").value))
						'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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						lstrDsCla = Trim(IIf(IsDbNull(.Fields("descricao_classe").value), "0000", .Fields("descricao_classe").value))
						'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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						ldblNumNF = CDbl(IIf(IsDbNull(.Fields("numnf").value), "0", .Fields("numnf").value))
						'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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						lstrMesAn = Trim(IIf(IsDbNull(.Fields("mesano").value), "00/0000", .Fields("mesano").value))
						'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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						llngMARef = CInt(IIf(IsDbNull(.Fields("mesano_ref").value), "0", .Fields("mesano_ref").value))
						'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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						ldteEmiss = CDate(IIf(IsDbNull(.Fields("dataem").value), "1900-01-01", .Fields("dataem").value))
						'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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						ldteApres = CDate(IIf(IsDbNull(.Fields("dataapr").value), "1900-01-01", .Fields("dataapr").value))
						'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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						ldteVecto = CDate(IIf(IsDbNull(.Fields("datavenc").value), "1900-01-01", .Fields("datavenc").value))
						'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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						ldteLeAtu = CDate(IIf(IsDbNull(.Fields("dataleit").value), "1900-01-01", .Fields("dataleit").value))
						'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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						ldteLeAnt = CDate(IIf(IsDbNull(.Fields("dataleitant").value), "1900-01-01", .Fields("dataleitant").value))
						'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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						lstrGrTar = Trim(IIf(IsDbNull(.Fields("ta").value), "01B100", .Fields("ta").value))
						'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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						lstrGrupo = Trim(IIf(IsDbNull(.Fields("grupo").value), "B", .Fields("grupo").value))
						'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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						lstrItens = Trim(IIf(IsDbNull(.Fields("itensserv").value), "", .Fields("itensserv").value))
						'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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						lintIrrig = CShort(IIf(IsDbNull(.Fields("irrigacao_aquicultura").value), "0", .Fields("irrigacao_aquicultura").value))
						If lstrGrupo = "B" Then
							ldblQtkWh = BuscaVlrItem(lstrItens, "030201009")
							ldblVlkWh = BuscaVlrItem(lstrItens, "030202009")
						End If
						
						'Rural, Baixa renda
						'Valor normal - Valor do residencial normal
						
						'Serviço Público
						'Tarifa normal SP - Desconto de 15%
						
						'Livre
						'Soma dos descontos
						
						'Irrigação
						'Soma dos descontos
						
						'Classes
						llngClass = 0
						Select Case CDbl(VB.Left(lstrClass, 1)) + 0
							Case Is < 4 'Industrial
								llngClass = 2
							Case Is = 4 'Comercial
								llngClass = 3
							Case Is = 5 'Rural
								llngClass = 4
							Case Is = 6 'Poderes Públicos
								llngClass = 5
								If CDbl(VB.Left(lstrClass, 1)) + 0 = 64 Then 'Iluminação Pública
									llngClass = 6
								End If
							Case Is = 7 'Serviço Público
								llngClass = 7
							Case Is = 8 'Consumo Próprio
								llngClass = 8
							Case Is = 9 'Residenciais - Somente baixa renda
								If lstrClass = "9102" Or lstrClass = "9104" Or lstrClass = "9105" Or lstrClass = "9106" Then
									llngClass = 1
								End If
						End Select
						'Tipos de desconto
						llngTipos = 0
						'llngTipos = 3 'Cooperativa de Eletrificação Rural
						'llngTipos = 4 'Serviço Público de Irrigação Rural
						'llngTipos = 6 'Irrigação Aquicultura em horário especial
						'llngTipos = 7 'Geração Fonte Incentivada
						'Descontos
						If lstrGrupo = "B" Then
							If llngClass = 4 Then 'Rural
								llngTipos = 2 'Rural
								ldblTarNr = TarifaProporcional(ldteLeAnt, ldteLeAtu, "01B1011101", 3)
								ldblNVlkWh = Truncate(ldblQtkWh * (ldblTarNr / 1000), 2)
								ldblVrDes = ldblVlkWh - ldblNVlkWh
								If lintIrrig = 1 Then
									ldblVrIrr = BuscaValorItem(lstrItens, "9070000009")
								Else
									ldblVrIrr = 0
								End If
								lblnInsere = True
							ElseIf llngClass = 1 Then  'Baixa Renda
								llngTipos = 1 'Baixa Renda
								ldblTarNr = TarifaProporcional(ldteLeAnt, ldteLeAtu, "01B1011101", 3)
								ldblNVlkWh = Truncate(ldblQtkWh * (ldblTarNr / 1000), 2)
								ldblVrDes = ldblVlkWh - ldblNVlkWh
								lblnInsere = True
							ElseIf llngClass = 7 Then  'Serviço Público
								llngTipos = 5
								ldblTarNr = TarifaProporcional(ldteLeAnt, ldteLeAtu, "01B3201101", 3) 'Tarifa Serviço Público Cheia
								ldblNVlkWh = Truncate(ldblQtkWh * (ldblTarNr / 1000), 2)
								ldblVrDes = ldblVlkWh - ldblNVlkWh
								lblnInsere = True
							End If
						ElseIf lstrGrupo = "A" Then 
							If lstrGrTar = "01AL00" Then
								llngTipos = 8 'Consumidor Livre - Fonte Incentivada
								
								ldblVlrLddp = BuscaVlrItem(lstrItens, "0302020051") * lintMpr 'Desconto Provisorio
								ldblVlrLddr = BuscaVlrItem(lstrItens, "0302020052") * lintMpr 'Desconto real
								ldblVlrLddd = BuscaVlrItem(lstrItens, "0302020053") * lintMpr 'diferença a devolver
								ldblVlrLdcd = BuscaVlrItem(lstrItens, "0302020054") * lintMpr 'diferença a cobrar
								
								ldblVlrLedp = BuscaVlrItem(lstrItens, "0302020055") * lintMpr 'Desconto Provisorio
								ldblVlrLedr = BuscaVlrItem(lstrItens, "0302020056") * lintMpr 'Desconto real
								ldblVlrLedd = BuscaVlrItem(lstrItens, "0302020057") * lintMpr 'diferença a devolver
								ldblVlrLecd = BuscaVlrItem(lstrItens, "0302020058") * lintMpr 'diferença a cobrar
								
								ldblVrDes = 0
								ldblVrDes = ldblVrDes + ldblVlrLddp 'Desconto Provisorio
								ldblVrDes = ldblVrDes + ldblVlrLddr 'Desconto real
								ldblVrDes = ldblVrDes + ldblVlrLddd 'diferença a devolver
								ldblVrDes = ldblVrDes - ldblVlrLdcd 'diferença a cobrar
								'
								ldblVrDes = ldblVrDes + ldblVlrLedp 'Desconto Provisorio
								ldblVrDes = ldblVrDes + ldblVlrLedr 'Desconto real
								ldblVrDes = ldblVrDes + ldblVlrLedd 'diferença a devolver
								ldblVrDes = ldblVrDes - ldblVlrLecd 'diferença a cobrar
								lblnInsere = True
							ElseIf lstrGrTar = "01AX00" Then 
								llngTipos = 9 'Distribuição
								ldblQkWhP = BuscaVlrItem(lstrItens, "0302010010")
								ldblQkWhF = BuscaVlrItem(lstrItens, "0302010011")
								ldblVkWhp = BuscaVlrItem(lstrItens, "0302020010")
								ldblVkWhf = BuscaVlrItem(lstrItens, "0302020011")
								ldblTaNrP = TarifaProporcional(ldteLeAnt, ldteLeAtu, "01AX011101", 3, "", 2)
								ldblTaNrF = TarifaProporcional(ldteLeAnt, ldteLeAtu, "01AX011102", 3, "", 2)
								ldblNVkWhP = Truncate(ldblQkWhP * (ldblTaNrP / 1000), 2)
								ldblNVkWhF = Truncate(ldblQkWhF * (ldblTaNrF / 1000), 2)
								ldblVrDes = (ldblVkWhp - ldblNVkWhP) + (ldblVkWhf - ldblNVkWhF)
								lblnInsere = True
							End If
						End If
						If lblnInsere Then
							lstrSql = ""
							lstrSql = lstrSql & "INSERT INTO tmp_exporta_subvencoes SET "
							lstrSql = lstrSql & "idagente = '" & gstrEmpresa_CodRes89 & "', "
							lstrSql = lstrSql & "nome = '" & Replace(lstrNomeC, "'", "\'") & "', "
							If IsNumeric(lstrCpfCnpj) Then
								lstrSql = lstrSql & "cpf = '" & IIf(Len(lstrCpfCnpj) <= 11, lstrCpfCnpj, "") & "', "
								lstrSql = lstrSql & "cnpj = '" & IIf(Len(lstrCpfCnpj) > 11, lstrCpfCnpj, "") & "', "
							Else
								lstrSql = lstrSql & "cpf = '', "
								lstrSql = lstrSql & "cnpj = '', "
							End If
							lstrSql = lstrSql & "uc = '" & lstrIdent & "', "
							lstrSql = lstrSql & "ibge = '" & BuscaIbge(0, "", BuscaNmCidade(lstrZona, "", 0)) & "', "
							lstrSql = lstrSql & "grupo = '" & lstrGrupo & "', "
							lstrSql = lstrSql & "classe = '" & llngClass & "', "
							lstrSql = lstrSql & "tipo = '" & llngTipos & "', "
							lstrSql = lstrSql & "ano = '" & VB.Right(lstrMesAn, 4) & "', "
							lstrSql = lstrSql & "mes = '" & VB.Left(lstrMesAn, 2) & "', "
							lstrSql = lstrSql & "desconto= " & Replace(CStr(ldblVrDes * IIf(ldblVrDes < 0, -1, 1)), ",", ".")
							Call Executar(lstrSql)
							If lintIrrig = 1 And ldblVrIrr <> 0 Then
								lstrSql = ""
								lstrSql = lstrSql & "INSERT INTO tmp_exporta_subvencoes SET "
								lstrSql = lstrSql & "idagente = '" & gstrEmpresa_CodRes89 & "', "
								lstrSql = lstrSql & "nome = '" & Replace(lstrNomeC, "'", "\'") & "', "
								If IsNumeric(lstrCpfCnpj) Then
									lstrSql = lstrSql & "cpf = '" & IIf(Len(lstrCpfCnpj) <= 11, lstrCpfCnpj, "") & "', "
									lstrSql = lstrSql & "cnpj = '" & IIf(Len(lstrCpfCnpj) > 11, lstrCpfCnpj, "") & "', "
								Else
									lstrSql = lstrSql & "cpf = '', "
									lstrSql = lstrSql & "cnpj = '', "
								End If
								lstrSql = lstrSql & "uc = '" & lstrIdent & "', "
								lstrSql = lstrSql & "ibge = '" & BuscaIbge(0, "", BuscaNmCidade(lstrZona, "", 0)) & "', "
								lstrSql = lstrSql & "grupo = '" & lstrGrupo & "', "
								lstrSql = lstrSql & "classe = '" & llngClass & "', "
								lstrSql = lstrSql & "tipo = '" & 6 & "', "
								lstrSql = lstrSql & "ano = '" & VB.Right(lstrMesAn, 4) & "', "
								lstrSql = lstrSql & "mes = '" & VB.Left(lstrMesAn, 2) & "', "
								lstrSql = lstrSql & "desconto = " & Replace(CStr(ldblVrIrr * IIf(ldblVrIrr < 0, -1, 1)), ",", ".")
								Call Executar(lstrSql)
							End If
							lstrSql = ""
							lstrSql = lstrSql & "INSERT INTO tmp_exporta_subvencoes_det SET "
							lstrSql = lstrSql & "numero_fatura = " & ldblNumNF & ", "
							lstrSql = lstrSql & "mes_ano = '" & lstrMesAn & "', "
							lstrSql = lstrSql & "zlc = '" & lstrIdent & "', "
							lstrSql = lstrSql & "qtd_kwh = " & Replace(CStr(ldblQtkWh), ",", ".") & ", "
							lstrSql = lstrSql & "qtd_kwh_ponta = " & Replace(CStr(ldblQkWhP), ",", ".") & ", "
							lstrSql = lstrSql & "qtd_kwh_fora_ponta = " & Replace(CStr(ldblQkWhF), ",", ".") & ", "
							lstrSql = lstrSql & "vlr_kwh = " & Replace(CStr(ldblVlkWh), ",", ".") & ", "
							lstrSql = lstrSql & "vlr_kwh_ponta = " & Replace(CStr(ldblVkWhp), ",", ".") & ", "
							lstrSql = lstrSql & "vlr_kwh_fora_ponta = " & Replace(CStr(ldblVkWhf), ",", ".") & ", "
							lstrSql = lstrSql & "tar_normal = " & Replace(CStr(ldblTarNr), ",", ".") & ", "
							lstrSql = lstrSql & "tar_normal_ponta = " & Replace(CStr(ldblTaNrP), ",", ".") & ", "
							lstrSql = lstrSql & "tar_normal_fora_ponta = " & Replace(CStr(ldblTaNrF), ",", ".") & ", "
							lstrSql = lstrSql & "vlr_kwh_normal = " & Replace(CStr(ldblNVlkWh), ",", ".") & ", "
							lstrSql = lstrSql & "vlr_kwh_normal_ponta = " & Replace(CStr(ldblNVkWhP), ",", ".") & ", "
							lstrSql = lstrSql & "vlr_kwh_normal_fora_ponta = " & Replace(CStr(ldblNVkWhF), ",", ".") & ", "
							lstrSql = lstrSql & "desconto = " & Replace(CStr(ldblVrDes), ",", ".") & ", "
							lstrSql = lstrSql & "irrigante = " & Replace(CStr(ldblVrIrr), ",", ".") & ", "
							lstrSql = lstrSql & "livre_desc_dem_prov = " & Replace(CStr(ldblVlrLddp), ",", ".") & ", "
							lstrSql = lstrSql & "livre_desc_dem_real = " & Replace(CStr(ldblVlrLddr), ",", ".") & ", "
							lstrSql = lstrSql & "livre_dif_dem_devolver = " & Replace(CStr(ldblVlrLddd), ",", ".") & ", "
							lstrSql = lstrSql & "livre_cob_dem_devolver = " & Replace(CStr(ldblVlrLdcd), ",", ".") & ", "
							lstrSql = lstrSql & "livre_desc_ene_prov = " & Replace(CStr(ldblVlrLedp), ",", ".") & ", "
							lstrSql = lstrSql & "livre_desc_ene_real = " & Replace(CStr(ldblVlrLedr), ",", ".") & ", "
							lstrSql = lstrSql & "livre_dif_ene_devolver = " & Replace(CStr(ldblVlrLedd), ",", ".") & ", "
							lstrSql = lstrSql & "livre_cob_ene_devolver = " & Replace(CStr(ldblVlrLecd), ",", ".") & ", "
							lstrSql = lstrSql & "dt_leit_atual = '" & VB6.Format(ldteLeAtu, "yyyy-MM-dd") & "', "
							lstrSql = lstrSql & "dt_leit_anterior = '" & VB6.Format(ldteLeAtu, "yyyy-MM-dd") & "', "
							lstrSql = lstrSql & "classe = '" & lstrClass & "', "
							lstrSql = lstrSql & "des_classe = '" & Replace(lstrDsCla, "'", "\'") & "', "
							lstrSql = lstrSql & "grupo = '" & lstrGrupo & "', "
							lstrSql = lstrSql & "gr_tar = '" & lstrGrTar & "' "
							Call Executar(lstrSql)
						End If
						Call .MoveNext()
						Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " " & (VB.Right(CStr(alngAnoMes), 2) & "/" & VB.Left(CStr(alngAnoMes), 4)) & " ")
					End While
				End If
				Call .Close()
			End If
		End With
	End Sub
	Private Sub dtpMesAno_Change(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles dtpMesAno.Change
		Dim Index As Short = dtpMesAno.GetIndex(eventSender)
		Call NomeArquivo()
	End Sub
	
	Private Sub DTPMesAno_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles DTPMesAno.Leave
		Dim Index As Short = DTPMesAno.GetIndex(eventSender)
		Me.dtpMesAno(1).value = CDate("31/12/" & Me.dtpMesAno(0).Year)
		Call NomeArquivo()
	End Sub
	
	Private Sub frmExportaSubvencoes_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 frmExportaSubvencoes_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		ifrmRelAuxiliar = New FrmRelatorio
		Call CenterForm(Me)
		Me.dtpMesAno(0).value = CDate("01/01/" & Year(BuscaDataBD))
		Me.dtpMesAno(1).value = CDate("31/12/" & Me.dtpMesAno(0).Year)
		istrNmArq = My.Application.Info.DirectoryPath & "\CDE" & gstrEmpresa_CodRes89 & "_" & VB6.Format(Me.dtpMesAno(0).value, "YYYYMM") & "_" & VB6.Format(Me.dtpMesAno(1).value, "YYYYMM") & ".CSV"
		Me.txtArquivo.Text = istrNmArq
		Call NomeArquivo()
	End Sub
	
	Private Sub frmExportaSubvencoes_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
		'UPGRADE_NOTE: Object ifrmRelAuxiliar 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"'
		ifrmRelAuxiliar = Nothing
	End Sub
	
	Private Sub cmdArquivo_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdArquivo.Click
		Dim lstrAux As String
		Call NomeArquivo()
		'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 = StrReverse(Mid(StrReverse(Me.txtArquivo.Text), CInt(InStr(StrReverse(Me.txtArquivo.Text), "\")))) & istrNmArq & "." & Me.dialArqOpen.DefaultExt
		Me.dialArqOpen.Title = "Arquivo de exportação Conta de Desenvolvimento Energético"
		Me.dialArqOpen.ShowDialog()
		lstrAux = StrReverse(Me.dialArqOpen.FileName)
		Me.txtArquivo.Text = StrReverse(Mid(lstrAux, CInt(InStr(lstrAux, "\")))) & istrNmArq & "." & Me.dialArqOpen.DefaultExt
	End Sub
	
	Private Sub NomeArquivo()
		Dim lstrAux As String
		Me.dialArqOpen.DefaultExt = "CSV"
		istrNmArq = "CDE" & gstrEmpresa_CodRes89 & "_" & VB6.Format(Me.dtpMesAno(0).value, "YYYYMM") & "_" & VB6.Format(Me.dtpMesAno(1).value, "YYYYMM")
		lstrAux = StrReverse(Me.txtArquivo.Text)
		Me.txtArquivo.Text = StrReverse(Mid(lstrAux, CInt(InStr(lstrAux, "\")))) & istrNmArq & "." & Me.dialArqOpen.DefaultExt
		System.Windows.Forms.Application.DoEvents()
	End Sub
	
	Private Sub LigDes(ByVal ablnModo As Boolean)
		Me.cmdExporta.Enabled = ablnModo
		Me.cmdFechar.Enabled = ablnModo
		Me.cmdImprimir.Enabled = ablnModo
		Me.cmdProcessar.Enabled = ablnModo
		Me.dtpMesAno(0).Enabled = ablnModo
		Me.dtpMesAno(1).Enabled = ablnModo
		Me.cmdArquivo.Enabled = ablnModo
		Me.chkConferencia.Enabled = ablnModo
	End Sub
	
	Private Sub CriaTabelas()
		Dim lstrSql As String
		Call Executar("DROP TEMPORARY TABLE IF EXISTS tmp_exporta_subvencoes")
		lstrSql = "CREATE TEMPORARY TABLE tmp_exporta_subvencoes ("
		lstrSql = lstrSql & "idagente varchar(4) DEFAULT '',"
		lstrSql = lstrSql & "nome VARCHAR(255) DEFAULT '',"
		lstrSql = lstrSql & "cpf VARCHAR(20) DEFAULT '',"
		lstrSql = lstrSql & "cnpj VARCHAR(20) DEFAULT '',"
		lstrSql = lstrSql & "uc VARCHAR(11) DEFAULT '',"
		lstrSql = lstrSql & "ibge INT(11) DEFAULT 0,"
		lstrSql = lstrSql & "grupo CHAR(1) DEFAULT '',"
		lstrSql = lstrSql & "classe INT(11) DEFAULT 0,"
		lstrSql = lstrSql & "tipo INT(11) DEFAULT 0,"
		lstrSql = lstrSql & "ano INT(11) DEFAULT 0,"
		lstrSql = lstrSql & "mes INT(11) DEFAULT 0,"
		lstrSql = lstrSql & "desconto DOUBLE (15,2) DEFAULT 0.00,"
		lstrSql = lstrSql & "KEY idxuc (uc),"
		lstrSql = lstrSql & "KEY idxano (ano),"
		lstrSql = lstrSql & "KEY idxmes (mes))"
		Call Executar(lstrSql)
		Call Executar("DROP TEMPORARY TABLE IF EXISTS tmp_exporta_subvencoes_det")
		lstrSql = "CREATE TEMPORARY TABLE tmp_exporta_subvencoes_det ("
		lstrSql = lstrSql & "numero_fatura double DEFAULT 0,"
		lstrSql = lstrSql & "mes_ano varchar(7) DEFAULT '',"
		lstrSql = lstrSql & "zlc varchar(11) DEFAULT '',"
		lstrSql = lstrSql & "qtd_kwh double(15,2) DEFAULT 0.00, "
		lstrSql = lstrSql & "qtd_kwh_ponta double(15,2) DEFAULT 0.00, "
		lstrSql = lstrSql & "qtd_kwh_fora_ponta double(15,2) DEFAULT 0.00, "
		lstrSql = lstrSql & "vlr_kwh double(15,2) DEFAULT 0.00, "
		lstrSql = lstrSql & "vlr_kwh_ponta double(15,2) DEFAULT 0.00, "
		lstrSql = lstrSql & "vlr_kwh_fora_ponta double(15,2) DEFAULT 0.00, "
		lstrSql = lstrSql & "tar_normal double(15,2) DEFAULT 0.00, "
		lstrSql = lstrSql & "tar_normal_ponta double(15,2) DEFAULT 0.00, "
		lstrSql = lstrSql & "tar_normal_fora_ponta double(15,2) DEFAULT 0.00, "
		lstrSql = lstrSql & "vlr_kwh_normal double(15,2) DEFAULT 0.00, "
		lstrSql = lstrSql & "vlr_kwh_normal_ponta double(15,2) DEFAULT 0.00, "
		lstrSql = lstrSql & "vlr_kwh_normal_fora_ponta double(15,2) DEFAULT 0.00, "
		lstrSql = lstrSql & "desconto double(15,2) DEFAULT 0.00, "
		lstrSql = lstrSql & "irrigante double(15,2) DEFAULT 0.00, "
		lstrSql = lstrSql & "livre_desc_dem_prov double(15,2) DEFAULT 0.00, "
		lstrSql = lstrSql & "livre_desc_dem_real double(15,2) DEFAULT 0.00, "
		lstrSql = lstrSql & "livre_dif_dem_devolver double(15,2) DEFAULT 0.00, "
		lstrSql = lstrSql & "livre_cob_dem_devolver double(15,2) DEFAULT 0.00, "
		lstrSql = lstrSql & "livre_desc_ene_prov double(15,2) DEFAULT 0.00, "
		lstrSql = lstrSql & "livre_desc_ene_real double(15,2) DEFAULT 0.00, "
		lstrSql = lstrSql & "livre_dif_ene_devolver double(15,2) DEFAULT 0.00, "
		lstrSql = lstrSql & "livre_cob_ene_devolver double(15,2) DEFAULT 0.00, "
		lstrSql = lstrSql & "dt_leit_atual date,"
		lstrSql = lstrSql & "dt_leit_anterior date,"
		lstrSql = lstrSql & "classe varchar(4) DEFAULT '',"
		lstrSql = lstrSql & "des_classe varchar(255) DEFAULT '',"
		lstrSql = lstrSql & "grupo char(1) DEFAULT '',"
		lstrSql = lstrSql & "gr_tar varchar(6) DEFAULT '',"
		lstrSql = lstrSql & "PRIMARY KEY (numero_fatura,mes_ano))"
		Call Executar(lstrSql)
	End Sub
	
	Private Sub cmdExporta_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExporta.Click
		'Controle Arquivo
		Dim lstrLin As String
		Dim lintARQ As Short
		Dim llngRow As Integer
		Dim llngCol As Integer
		'Controle de Query
		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!", MsgBoxStyle.Critical, gstrTituloMensagem)
		Else
			Call LigDes(False)
			'Export para CSV
			If Trim(Me.txtArquivo.Text) <> "" Then
				If Me.chkConferencia.CheckState = System.Windows.Forms.CheckState.Unchecked Then
					lstrSql = "SELECT * FROM tmp_exporta_subvencoes ORDER BY grupo ASC, uc ASC, ano ASC, mes ASC "
					Call Conectar(lstrSql, lrstAux)
					With lrstAux
						If .State = ADODB.ObjectStateEnum.adStateOpen Then
							If .RecordCount > 0 Then
								Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .RecordCount, " Gerando Arquivo ")
								lintARQ = FreeFile
								FileOpen(lintARQ, Me.txtArquivo.Text, OpenMode.Output)
								lstrLin = ""
								For llngCol = 0 To .Fields.Count - 1
									'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).Name), "", .Fields(llngCol).Name) & ";"
								Next llngCol
								PrintLine(lintARQ, lstrLin)
								While Not .EOF
									lstrLin = ""
									For llngCol = 0 To .Fields.Count - 1
										'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) & ";"
									Next llngCol
									PrintLine(lintARQ, lstrLin)
									Call .MoveNext()
									Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " Gerando Arquivo ")
								End While
								FileClose(lintARQ)
								Call MsgBox("Arquivo gerado com sucesso!", MsgBoxStyle.Information, gstrTituloMensagem)
							End If
							Call .Close()
						End If
					End With
				Else
					lstrSql = "SELECT * FROM tmp_exporta_subvencoes_det ORDER BY grupo ASC, zlc ASC, CONCAT(RIGHT(mes_ano,4),LEFT(mes_ano,2)) ASC "
					Call Conectar(lstrSql, lrstAux)
					With lrstAux
						If .State = ADODB.ObjectStateEnum.adStateOpen Then
							If .RecordCount > 0 Then
								Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .RecordCount, " Gerando Arquivo ")
								lintARQ = FreeFile
								FileOpen(lintARQ, Me.txtArquivo.Text, OpenMode.Output)
								lstrLin = ""
								For llngCol = 0 To .Fields.Count - 1
									'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).Name), "", .Fields(llngCol).Name) & ";"
								Next llngCol
								PrintLine(lintARQ, lstrLin)
								While Not .EOF
									lstrLin = ""
									For llngCol = 0 To .Fields.Count - 1
										'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) & ";"
									Next llngCol
									PrintLine(lintARQ, lstrLin)
									Call .MoveNext()
									Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " Gerando Arquivo ")
								End While
								FileClose(lintARQ)
								Call MsgBox("Arquivo gerado com sucesso!", MsgBoxStyle.Information, gstrTituloMensagem)
							End If
							Call .Close()
						End If
					End With
				End If
			Else
				Call MsgBox("Você precisa selecionar o caminho e o arquivo para exportação!", MsgBoxStyle.Information, gstrTituloMensagem)
			End If
			Call LigDes(True)
		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
End Class