Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frmGerarOficio312014
	Inherits System.Windows.Forms.Form
	
	Private Sub cmdArquivoClasse_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdArquivoClasse.Click
		Me.dialArqOpen.DefaultExt = "TXT"
		'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 = Me.txtArquivoClasse.Text
		Me.dialArqOpen.Title = "Arquivo de exportação de classes"
		Me.dialArqOpen.ShowDialog()
		Me.txtArquivoClasse.Text = Me.dialArqOpen.FileName
	End Sub
	
	Private Sub cmdArquivoFaturamento_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdArquivoFaturamento.Click
		Me.dialArqOpen.DefaultExt = "TXT"
		'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 = Me.txtArquivoFaturamento.Text
		Me.dialArqOpen.Title = "Arquivo de exportação de faturas"
		Me.dialArqOpen.ShowDialog()
		Me.txtArquivoFaturamento.Text = Me.dialArqOpen.FileName
	End Sub
	
	Private Sub cmdArquivoLigacao_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdArquivoLigacao.Click
		Me.dialArqOpen.DefaultExt = "TXT"
		'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 = Me.txtArquivoLigacao.Text
		Me.dialArqOpen.Title = "Arquivo de exportação de ligação"
		Me.dialArqOpen.ShowDialog()
		Me.txtArquivoLigacao.Text = Me.dialArqOpen.FileName
	End Sub
	
	Private Sub cmdArquivoReligacao_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdArquivoReligacao.Click
		Me.dialArqOpen.DefaultExt = "TXT"
		'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 = Me.txtArquivoReligacao.Text
		Me.dialArqOpen.Title = "Arquivo de exportação de religação"
		Me.dialArqOpen.ShowDialog()
		Me.txtArquivoReligacao.Text = Me.dialArqOpen.FileName
	End Sub
	
	Private Sub cmdArquivoRessarcimento_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdArquivoRessarcimento.Click
		Me.dialArqOpen.DefaultExt = "TXT"
		'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 = Me.txtArquivoRessarcimento.Text
		Me.dialArqOpen.Title = "Arquivo de exportação de dados elétricos"
		Me.dialArqOpen.ShowDialog()
		Me.txtArquivoRessarcimento.Text = Me.dialArqOpen.FileName
	End Sub
	
	Private Sub cmdExporta_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExporta.Click
		Dim lstrCdEmpr As String
		Dim lstrCdIbge As String
		Dim lstrNmIbge As String
		Dim llngAnoMes As Integer
		
		Dim ldblQtdAtivos As Double
		Dim ldblQtdMedia As Double
		Dim ldblQtdCusto As Double
		Dim ldblQtdSemFatura As Double
		Dim ldblQtdComFatura As Double
		
		Dim ldblQtdClaseRu As Double
		Dim ldblQtdClaseRe As Double
		Dim ldblQtdClaseIn As Double
		Dim ldblQtdClaseSp As Double
		Dim ldblQtdClasePp As Double
		Dim ldblQtdClaseIp As Double
		Dim ldblQtdClaseCo As Double
		Dim ldblQtdClaseCp As Double
		
		Dim ldblQtdDanosEletricos As Double
		Dim ldblQtdDanosEletricosProcedentes As Double
		
		Dim ldblQtd_b_rural_ligacoes As Double
		Dim ldblQtd_b_rural_ligacoes_fora_prazo As Double
		Dim ldblQtd_b_urbano_ligacoes As Double
		Dim ldblQtd_b_urbano_ligacoes_fora_prazo As Double
		Dim ldblQtd_b_rural_ligacoes_pendentes As Double
		Dim ldblQtd_b_urbano_ligacoes_pendentes As Double
		Dim ldblQtd_a_ligacoes As Double
		Dim ldblQtd_a_ligacoes_fora_prazo As Double
		Dim ldblQtd_a_ligacoes_pendentes As Double
		
		Dim ldblQtd_b_rural_religacao_urgencia As Double
		Dim ldblQtd_b_rural_religacao_urgencia_fora_prazo As Double
		Dim ldblQtd_b_urbano_religacao_urgencia As Double
		Dim ldblQtd_b_urbano_religacao_urgencia_fora_prazo As Double
		Dim ldblQtd_a_religacao_urgencia As Double
		Dim ldblQtd_a_religacao_urgencia_fora_prazo As Double
		Dim ldblQtd_b_rural_religacao_normal As Double
		Dim ldblQtd_b_rural_religacao_normal_fora_prazo As Double
		Dim ldblQtd_b_urbano_religacao_normal As Double
		Dim ldblQtd_b_urbano_religacao_normal_fora_prazo As Double
		Dim ldblQtd_a_religacao_normal As Double
		Dim ldblQtd_a_religacao_normal_fora_prazo As Double
		
		Dim lstrLin As String
		Dim lintARQ As Short
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		If Me.chkFaturas.CheckState = System.Windows.Forms.CheckState.Checked Then
			If Trim(Me.txtArquivoFaturamento.Text) <> "" Then
				Me.lblInfo1.Text = " Buscando as faturas "
				Me.lblInfo1.Refresh()
				lstrSql = ""
				lstrSql = lstrSql & "SELECT `cdempresa`,`anomes`,`ibge`,`nomeibge`, SUM(`ligados`) ativos, "
				lstrSql = lstrSql & "        SUM(`media`) media, SUM(`custo`) custo, SUM(`sem_fatura`) semfatura, SUM(`com_fatura`) comfatura "
				lstrSql = lstrSql & "  FROM `oficio_31_2014` "
				lstrSql = lstrSql & " GROUP BY `cdempresa` ASC,`anomes` ASC, `ibge` ASC ,`nomeibge` ASC "
				Call Conectar(lstrSql, lrstAux)
				With lrstAux
					If .State = ADODB.ObjectStateEnum.adStateOpen Then
						If .RecordCount > 0 Then
							lintARQ = FreeFile
							FileOpen(lintARQ, Me.txtArquivoFaturamento.Text, OpenMode.Output)
							Call BarraProgresso((Me.ProgressBar1), (Me.lblPerc1), (Me.lblInfo1), 0, .RecordCount, " Gerando Arquivo Faturas ")
							While Not .EOF
								'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"'
								lstrCdEmpr = IIf(IsDbNull(.Fields("cdempresa").value), "", .Fields("cdempresa").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"'
								lstrCdIbge = IIf(IsDbNull(.Fields("ibge").value), "", .Fields("ibge").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"'
								lstrNmIbge = IIf(IsDbNull(.Fields("nomeibge").value), "", .Fields("nomeibge").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"'
								llngAnoMes = IIf(IsDbNull(.Fields("anomes").value), 0, .Fields("anomes").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"'
								ldblQtdAtivos = IIf(IsDbNull(.Fields("ativos").value), 0, .Fields("ativos").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"'
								ldblQtdMedia = IIf(IsDbNull(.Fields("media").value), 0, .Fields("media").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"'
								ldblQtdCusto = IIf(IsDbNull(.Fields("custo").value), 0, .Fields("custo").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"'
								ldblQtdSemFatura = IIf(IsDbNull(.Fields("semfatura").value), 0, .Fields("semfatura").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"'
								ldblQtdComFatura = IIf(IsDbNull(.Fields("comfatura").value), 0, .Fields("comfatura").value)
								lstrLin = ""
								lstrLin = lstrLin & Trim(lstrCdEmpr) & ";"
								lstrLin = lstrLin & Trim(VB.Left(CStr(llngAnoMes), 4)) & ";"
								lstrLin = lstrLin & Trim(VB.Right(CStr(llngAnoMes), 2)) & ";"
								lstrLin = lstrLin & Trim(lstrNmIbge) & ";"
								lstrLin = lstrLin & Trim(lstrCdIbge) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtdAtivos, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtdMedia, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtdCusto, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtdSemFatura, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtdComFatura, "#,##0"), ".", "")) & ";"
								PrintLine(lintARQ, lstrLin)
								Call .MoveNext()
								Call BarraProgresso((Me.ProgressBar1), (Me.lblPerc1), (Me.lblInfo1), 1,  , " Gerando Arquivo Faturas ")
							End While
							FileClose(lintARQ)
						End If
						Call .Close()
					End If
				End With
			End If
			If Trim(Me.txtArquivoClasse.Text) <> "" Then
				Me.lblInfo1.Text = " Buscando as classes "
				Me.lblInfo1.Refresh()
				lstrSql = ""
				lstrSql = lstrSql & "SELECT `cdempresa`,`anomes`,`ibge`,`nomeibge`, SUM(`ligados`) ativos, "
				lstrSql = lstrSql & "        SUM(`classe_ru`) classe_ru, SUM(`classe_re`) classe_re, "
				lstrSql = lstrSql & "        SUM(`classe_in`) classe_in, SUM(`classe_sp`) classe_sp, "
				lstrSql = lstrSql & "        SUM(`classe_pp`) classe_pp, SUM(`classe_ip`) classe_ip, "
				lstrSql = lstrSql & "        SUM(`classe_co`) classe_co, SUM(`classe_cp`) classe_cp "
				lstrSql = lstrSql & "  FROM `oficio_31_2014` "
				lstrSql = lstrSql & " GROUP BY `cdempresa` ASC,`anomes` ASC, `ibge` ASC ,`nomeibge` ASC "
				Call Conectar(lstrSql, lrstAux)
				With lrstAux
					If .State = ADODB.ObjectStateEnum.adStateOpen Then
						If .RecordCount > 0 Then
							lintARQ = FreeFile
							FileOpen(lintARQ, Me.txtArquivoClasse.Text, OpenMode.Output)
							Call BarraProgresso((Me.ProgressBar1), (Me.lblPerc1), (Me.lblInfo1), 0, .RecordCount, " Gerando Arquivo Classes ")
							While Not .EOF
								'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"'
								lstrCdEmpr = IIf(IsDbNull(.Fields("cdempresa").value), "", .Fields("cdempresa").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"'
								lstrCdIbge = IIf(IsDbNull(.Fields("ibge").value), "", .Fields("ibge").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"'
								lstrNmIbge = IIf(IsDbNull(.Fields("nomeibge").value), "", .Fields("nomeibge").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"'
								llngAnoMes = IIf(IsDbNull(.Fields("anomes").value), 0, .Fields("anomes").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"'
								ldblQtdAtivos = IIf(IsDbNull(.Fields("ativos").value), 0, .Fields("ativos").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"'
								ldblQtdClaseRu = IIf(IsDbNull(.Fields("classe_ru").value), 0, .Fields("classe_ru").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"'
								ldblQtdClaseRe = IIf(IsDbNull(.Fields("classe_re").value), 0, .Fields("classe_re").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"'
								ldblQtdClaseIn = IIf(IsDbNull(.Fields("classe_in").value), 0, .Fields("classe_in").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"'
								ldblQtdClaseSp = IIf(IsDbNull(.Fields("classe_sp").value), 0, .Fields("classe_sp").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"'
								ldblQtdClasePp = IIf(IsDbNull(.Fields("classe_pp").value), 0, .Fields("classe_pp").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"'
								ldblQtdClaseIp = IIf(IsDbNull(.Fields("classe_ip").value), 0, .Fields("classe_ip").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"'
								ldblQtdClaseCo = IIf(IsDbNull(.Fields("classe_co").value), 0, .Fields("classe_co").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"'
								ldblQtdClaseCp = IIf(IsDbNull(.Fields("classe_cp").value), 0, .Fields("classe_cp").value)
								lstrLin = ""
								lstrLin = lstrLin & Trim(lstrCdEmpr) & ";"
								lstrLin = lstrLin & Trim(VB.Left(CStr(llngAnoMes), 4)) & ";"
								lstrLin = lstrLin & Trim(VB.Right(CStr(llngAnoMes), 2)) & ";"
								lstrLin = lstrLin & Trim(lstrNmIbge) & ";"
								lstrLin = lstrLin & Trim(lstrCdIbge) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtdAtivos, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtdClaseRu, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtdClaseRe, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtdClaseIn, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtdClaseSp, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtdClasePp, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtdClaseIp, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtdClaseCo, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtdClaseCp, "#,##0"), ".", "")) & ";"
								PrintLine(lintARQ, lstrLin)
								Call .MoveNext()
								Call BarraProgresso((Me.ProgressBar1), (Me.lblPerc1), (Me.lblInfo1), 1,  , " Gerando Arquivo Classes ")
							End While
							FileClose(lintARQ)
						End If
						Call .Close()
					End If
				End With
			End If
		End If
		If Me.chkReclamacao.CheckState = System.Windows.Forms.CheckState.Checked Then
			If Trim(Me.txtArquivoRessarcimento.Text) <> "" Then
				Me.lblInfo1.Text = " Buscando as danos elétricos "
				Me.lblInfo1.Refresh()
				lstrSql = ""
				lstrSql = lstrSql & "SELECT `cdempresa`,`anomes`,`ibge`,`nomeibge`, SUM(`ligados`) ativos, "
				lstrSql = lstrSql & "        SUM(`danos_eletricos`) danos_eletricos, SUM(`danos_eletricos_procedente`) danos_eletricos_procedente "
				lstrSql = lstrSql & "  FROM `oficio_31_2014` "
				lstrSql = lstrSql & " GROUP BY `cdempresa` ASC,`anomes` ASC, `ibge` ASC ,`nomeibge` ASC "
				Call Conectar(lstrSql, lrstAux)
				With lrstAux
					If .State = ADODB.ObjectStateEnum.adStateOpen Then
						If .RecordCount > 0 Then
							lintARQ = FreeFile
							FileOpen(lintARQ, Me.txtArquivoRessarcimento.Text, OpenMode.Output)
							Call BarraProgresso((Me.ProgressBar1), (Me.lblPerc1), (Me.lblInfo1), 0, .RecordCount, " Gerando Arquivo Danos Eletricos ")
							While Not .EOF
								'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"'
								lstrCdEmpr = IIf(IsDbNull(.Fields("cdempresa").value), "", .Fields("cdempresa").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"'
								lstrCdIbge = IIf(IsDbNull(.Fields("ibge").value), "", .Fields("ibge").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"'
								lstrNmIbge = IIf(IsDbNull(.Fields("nomeibge").value), "", .Fields("nomeibge").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"'
								llngAnoMes = IIf(IsDbNull(.Fields("anomes").value), 0, .Fields("anomes").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"'
								ldblQtdAtivos = IIf(IsDbNull(.Fields("ativos").value), 0, .Fields("ativos").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"'
								ldblQtdDanosEletricos = IIf(IsDbNull(.Fields("danos_eletricos").value), 0, .Fields("danos_eletricos").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"'
								ldblQtdDanosEletricosProcedentes = IIf(IsDbNull(.Fields("danos_eletricos_procedente").value), 0, .Fields("danos_eletricos_procedente").value)
								lstrLin = ""
								lstrLin = lstrLin & Trim(lstrCdEmpr) & ";"
								lstrLin = lstrLin & Trim(VB.Left(CStr(llngAnoMes), 4)) & ";"
								lstrLin = lstrLin & Trim(VB.Right(CStr(llngAnoMes), 2)) & ";"
								lstrLin = lstrLin & Trim(lstrNmIbge) & ";"
								lstrLin = lstrLin & Trim(lstrCdIbge) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtdAtivos, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtdDanosEletricos, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtdDanosEletricosProcedentes, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(0, "#,##0"), ".", "")) & ";" 'Menor Valor
								lstrLin = lstrLin & Trim(Replace(VB6.Format(0, "#,##0"), ".", "")) & ";" 'Maior Valor
								lstrLin = lstrLin & Trim(Replace(VB6.Format(0, "#,##0"), ".", "")) & ";" 'Valor Médio
								lstrLin = lstrLin & Trim(Replace(VB6.Format(0, "#,##0"), ".", "")) & ";" 'Valor Total
								PrintLine(lintARQ, lstrLin)
								Call .MoveNext()
								Call BarraProgresso((Me.ProgressBar1), (Me.lblPerc1), (Me.lblInfo1), 1,  , " Gerando Arquivo Danos Eletricos ")
							End While
							FileClose(lintARQ)
						End If
						Call .Close()
					End If
				End With
			End If
		End If
		If Me.chkOS.CheckState = System.Windows.Forms.CheckState.Checked Then
			If Trim(Me.txtArquivoLigacao.Text) <> "" Then
				Me.lblInfo1.Text = " Buscando as ligações "
				Me.lblInfo1.Refresh()
				lstrSql = ""
				lstrSql = lstrSql & "SELECT `cdempresa`,`anomes`,`ibge`,`nomeibge`, SUM(`ligados`) ativos, "
				lstrSql = lstrSql & "        SUM(`b_rural_ligacoes`) b_rural_ligacoes, SUM(`b_rural_ligacoes_fora_prazo`) b_rural_ligacoes_fora_prazo, "
				lstrSql = lstrSql & "        SUM(`b_urbano_ligacoes`) b_urbano_ligacoes, SUM(`b_urbano_ligacoes_fora_prazo`) b_urbano_ligacoes_fora_prazo, "
				lstrSql = lstrSql & "        SUM(`b_rural_ligacoes_pendentes`) b_rural_ligacoes_pendentes, SUM(`b_urbano_ligacoes_pendentes`) b_urbano_ligacoes_pendentes, "
				lstrSql = lstrSql & "        SUM(`a_ligacoes`) a_ligacoes, SUM(`a_ligacoes_fora_prazo`) a_ligacoes_fora_prazo, "
				lstrSql = lstrSql & "        SUM(`a_ligacoes_pendentes`) a_ligacoes_pendentes "
				lstrSql = lstrSql & "  FROM `oficio_31_2014` "
				lstrSql = lstrSql & " GROUP BY `cdempresa` ASC,`anomes` ASC, `ibge` ASC ,`nomeibge` ASC "
				Call Conectar(lstrSql, lrstAux)
				With lrstAux
					If .State = ADODB.ObjectStateEnum.adStateOpen Then
						If .RecordCount > 0 Then
							lintARQ = FreeFile
							FileOpen(lintARQ, Me.txtArquivoLigacao.Text, OpenMode.Output)
							Call BarraProgresso((Me.ProgressBar1), (Me.lblPerc1), (Me.lblInfo1), 0, .RecordCount, " Gerando Arquivo Ligações ")
							While Not .EOF
								'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"'
								lstrCdEmpr = IIf(IsDbNull(.Fields("cdempresa").value), "", .Fields("cdempresa").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"'
								lstrCdIbge = IIf(IsDbNull(.Fields("ibge").value), "", .Fields("ibge").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"'
								lstrNmIbge = IIf(IsDbNull(.Fields("nomeibge").value), "", .Fields("nomeibge").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"'
								llngAnoMes = IIf(IsDbNull(.Fields("anomes").value), 0, .Fields("anomes").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"'
								ldblQtdAtivos = IIf(IsDbNull(.Fields("ativos").value), 0, .Fields("ativos").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"'
								ldblQtd_b_rural_ligacoes = IIf(IsDbNull(.Fields("b_rural_ligacoes").value), 0, .Fields("b_rural_ligacoes").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"'
								ldblQtd_b_rural_ligacoes_fora_prazo = IIf(IsDbNull(.Fields("b_rural_ligacoes_fora_prazo").value), 0, .Fields("b_rural_ligacoes_fora_prazo").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"'
								ldblQtd_b_urbano_ligacoes = IIf(IsDbNull(.Fields("b_urbano_ligacoes").value), 0, .Fields("b_urbano_ligacoes").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"'
								ldblQtd_b_urbano_ligacoes_fora_prazo = IIf(IsDbNull(.Fields("b_urbano_ligacoes_fora_prazo").value), 0, .Fields("b_urbano_ligacoes_fora_prazo").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"'
								ldblQtd_b_rural_ligacoes_pendentes = IIf(IsDbNull(.Fields("b_rural_ligacoes_pendentes").value), 0, .Fields("b_rural_ligacoes_pendentes").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"'
								ldblQtd_b_urbano_ligacoes_pendentes = IIf(IsDbNull(.Fields("b_urbano_ligacoes_pendentes").value), 0, .Fields("b_urbano_ligacoes_pendentes").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"'
								ldblQtd_a_ligacoes = IIf(IsDbNull(.Fields("a_ligacoes").value), 0, .Fields("a_ligacoes").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"'
								ldblQtd_a_ligacoes_fora_prazo = IIf(IsDbNull(.Fields("a_ligacoes_fora_prazo").value), 0, .Fields("a_ligacoes_fora_prazo").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"'
								ldblQtd_a_ligacoes_pendentes = IIf(IsDbNull(.Fields("a_ligacoes_pendentes").value), 0, .Fields("a_ligacoes_pendentes").value)
								lstrLin = ""
								lstrLin = lstrLin & Trim(lstrCdEmpr) & ";"
								lstrLin = lstrLin & Trim(VB.Left(CStr(llngAnoMes), 4)) & ";"
								lstrLin = lstrLin & Trim(VB.Right(CStr(llngAnoMes), 2)) & ";"
								lstrLin = lstrLin & Trim(lstrNmIbge) & ";"
								lstrLin = lstrLin & Trim(lstrCdIbge) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtdAtivos, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtd_b_rural_ligacoes, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtd_b_rural_ligacoes_fora_prazo, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtd_b_urbano_ligacoes, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtd_b_urbano_ligacoes_fora_prazo, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtd_b_rural_ligacoes_pendentes, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtd_b_urbano_ligacoes_pendentes, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtd_a_ligacoes, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtd_a_ligacoes_fora_prazo, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtd_a_ligacoes_pendentes, "#,##0"), ".", "")) & ";"
								PrintLine(lintARQ, lstrLin)
								Call .MoveNext()
								Call BarraProgresso((Me.ProgressBar1), (Me.lblPerc1), (Me.lblInfo1), 1,  , " Gerando Arquivo Ligações ")
							End While
							FileClose(lintARQ)
						End If
						Call .Close()
					End If
				End With
			End If
			If Trim(Me.txtArquivoReligacao.Text) <> "" Then
				Me.lblInfo1.Text = " Buscando as religações "
				Me.lblInfo1.Refresh()
				lstrSql = ""
				lstrSql = lstrSql & "SELECT `cdempresa`,`anomes`,`ibge`,`nomeibge`, SUM(`ligados`) ativos, "
				lstrSql = lstrSql & "        SUM(`b_rural_religacao_urgencia`) b_rural_religacao_urgencia, "
				lstrSql = lstrSql & "        SUM(`b_rural_religacao_urgencia_fora_prazo`) b_rural_religacao_urgencia_fora_prazo, "
				lstrSql = lstrSql & "        SUM(`b_urbano_religacao_urgencia`) b_urbano_religacao_urgencia, "
				lstrSql = lstrSql & "        SUM(`b_urbano_religacao_urgencia_fora_prazo`) b_urbano_religacao_urgencia_fora_prazo, "
				
				lstrSql = lstrSql & "        SUM(`b_rural_religacao_normal`) b_rural_religacao_normal, "
				lstrSql = lstrSql & "        SUM(`b_rural_religacao_normal_fora_prazo`) b_rural_religacao_normal_fora_prazo, "
				lstrSql = lstrSql & "        SUM(`b_urbano_religacao_normal`) b_urbano_religacao_normal, "
				lstrSql = lstrSql & "        SUM(`b_urbano_religacao_normal_fora_prazo`) b_urbano_religacao_normal_fora_prazo, "
				
				lstrSql = lstrSql & "        SUM(`a_religacao_urgencia`) a_religacao_urgencia, "
				lstrSql = lstrSql & "        SUM(`a_religacao_urgencia_fora_prazo`) a_religacao_urgencia_fora_prazo, "
				lstrSql = lstrSql & "        SUM(`a_religacao_normal`) a_religacao_normal, "
				lstrSql = lstrSql & "        SUM(`a_religacao_normal_fora_prazo`) a_religacao_normal_fora_prazo "
				
				lstrSql = lstrSql & "  FROM `oficio_31_2014` "
				lstrSql = lstrSql & " GROUP BY `cdempresa` ASC,`anomes` ASC, `ibge` ASC ,`nomeibge` ASC "
				Call Conectar(lstrSql, lrstAux)
				With lrstAux
					If .State = ADODB.ObjectStateEnum.adStateOpen Then
						If .RecordCount > 0 Then
							lintARQ = FreeFile
							FileOpen(lintARQ, Me.txtArquivoReligacao.Text, OpenMode.Output)
							Call BarraProgresso((Me.ProgressBar1), (Me.lblPerc1), (Me.lblInfo1), 0, .RecordCount, " Gerando Arquivo Religações ")
							While Not .EOF
								'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"'
								lstrCdEmpr = IIf(IsDbNull(.Fields("cdempresa").value), "", .Fields("cdempresa").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"'
								lstrCdIbge = IIf(IsDbNull(.Fields("ibge").value), "", .Fields("ibge").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"'
								lstrNmIbge = IIf(IsDbNull(.Fields("nomeibge").value), "", .Fields("nomeibge").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"'
								llngAnoMes = IIf(IsDbNull(.Fields("anomes").value), 0, .Fields("anomes").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"'
								ldblQtdAtivos = IIf(IsDbNull(.Fields("ativos").value), 0, .Fields("ativos").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"'
								ldblQtd_b_rural_religacao_urgencia = IIf(IsDbNull(.Fields("b_rural_religacao_urgencia").value), 0, .Fields("b_rural_religacao_urgencia").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"'
								ldblQtd_b_rural_religacao_urgencia_fora_prazo = IIf(IsDbNull(.Fields("b_rural_religacao_urgencia_fora_prazo").value), 0, .Fields("b_rural_religacao_urgencia_fora_prazo").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"'
								ldblQtd_b_urbano_religacao_urgencia = IIf(IsDbNull(.Fields("b_urbano_religacao_urgencia").value), 0, .Fields("b_urbano_religacao_urgencia").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"'
								ldblQtd_b_urbano_religacao_urgencia_fora_prazo = IIf(IsDbNull(.Fields("b_urbano_religacao_urgencia_fora_prazo").value), 0, .Fields("b_urbano_religacao_urgencia_fora_prazo").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"'
								ldblQtd_b_rural_religacao_normal = IIf(IsDbNull(.Fields("b_rural_religacao_normal").value), 0, .Fields("b_rural_religacao_normal").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"'
								ldblQtd_b_rural_religacao_normal_fora_prazo = IIf(IsDbNull(.Fields("b_rural_religacao_normal_fora_prazo").value), 0, .Fields("b_rural_religacao_normal_fora_prazo").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"'
								ldblQtd_b_urbano_religacao_normal = IIf(IsDbNull(.Fields("b_urbano_religacao_normal").value), 0, .Fields("b_urbano_religacao_normal").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"'
								ldblQtd_b_urbano_religacao_normal_fora_prazo = IIf(IsDbNull(.Fields("b_urbano_religacao_normal_fora_prazo").value), 0, .Fields("b_urbano_religacao_normal_fora_prazo").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"'
								ldblQtd_a_religacao_urgencia = IIf(IsDbNull(.Fields("a_religacao_urgencia").value), 0, .Fields("a_religacao_urgencia").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"'
								ldblQtd_a_religacao_urgencia_fora_prazo = IIf(IsDbNull(.Fields("a_religacao_urgencia_fora_prazo").value), 0, .Fields("a_religacao_urgencia_fora_prazo").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"'
								ldblQtd_a_religacao_normal = IIf(IsDbNull(.Fields("a_religacao_normal").value), 0, .Fields("a_religacao_normal").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"'
								ldblQtd_a_religacao_normal_fora_prazo = IIf(IsDbNull(.Fields("a_religacao_normal_fora_prazo").value), 0, .Fields("a_religacao_normal_fora_prazo").value)
								
								
								lstrLin = ""
								lstrLin = lstrLin & Trim(lstrCdEmpr) & ";"
								lstrLin = lstrLin & Trim(VB.Left(CStr(llngAnoMes), 4)) & ";"
								lstrLin = lstrLin & Trim(VB.Right(CStr(llngAnoMes), 2)) & ";"
								lstrLin = lstrLin & Trim(lstrNmIbge) & ";"
								lstrLin = lstrLin & Trim(lstrCdIbge) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtdAtivos, "#,##0"), ".", "")) & ";"
								
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtd_b_rural_religacao_urgencia, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtd_b_rural_religacao_urgencia_fora_prazo, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtd_b_urbano_religacao_urgencia, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtd_b_urbano_religacao_urgencia_fora_prazo, "#,##0"), ".", "")) & ";"
								
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtd_b_rural_religacao_normal, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtd_b_rural_religacao_normal_fora_prazo, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtd_b_urbano_religacao_normal, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtd_b_urbano_religacao_normal_fora_prazo, "#,##0"), ".", "")) & ";"
								
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtd_a_religacao_urgencia, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtd_a_religacao_urgencia_fora_prazo, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtd_a_religacao_normal, "#,##0"), ".", "")) & ";"
								lstrLin = lstrLin & Trim(Replace(VB6.Format(ldblQtd_a_religacao_normal_fora_prazo, "#,##0"), ".", "")) & ";"
								
								PrintLine(lintARQ, lstrLin)
								Call .MoveNext()
								Call BarraProgresso((Me.ProgressBar1), (Me.lblPerc1), (Me.lblInfo1), 1,  , " Gerando Arquivo Religações  ")
							End While
							FileClose(lintARQ)
						End If
						Call .Close()
					End If
				End With
			End If
		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
		Call MsgBox("OK")
	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 Function VerificaSeExiste(ByVal alngAnoMes As Integer, ByVal astrZona As String, ByVal astrLivro As String, ByVal astrConta As String, ByVal alngUnidId As Integer) As Boolean
		Dim lblnRet As Boolean
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		lblnRet = False
		lstrSql = ""
		lstrSql = lstrSql & "SELECT * "
		lstrSql = lstrSql & "  FROM `oficio_31_2014` "
		lstrSql = lstrSql & " WHERE `anomes` = " & alngAnoMes & " "
		lstrSql = lstrSql & "   AND `unid_id` = " & alngUnidId & " "
		lstrSql = lstrSql & "   AND `zona` = '" & astrZona & "' "
		lstrSql = lstrSql & "   AND `livro` = '" & astrLivro & "' "
		lstrSql = lstrSql & "   AND `conta` = '" & astrConta & "' "
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				lblnRet = (.RecordCount > 0)
				Call .Close()
			End If
		End With
		'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
		VerificaSeExiste = lblnRet
	End Function
	
	Private Sub cmdProcessar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdProcessar.Click
		Dim lstrSql As String
		Dim lstrZon As String
		Dim lstrLiv As String
		Dim lstrCon As String
		Dim llngUId As Integer
		Dim llngCId As Integer
		Dim lstrGru As String
		Dim lstrReg As String
		Dim lstrNom As String
		Dim ldteLig As Date
		Dim lstrSit As String
		Dim lstrCla As String
		
		Dim lintCru As Short
		Dim lintCre As Short
		Dim lintCin As Short
		Dim lintCsp As Short
		Dim lintCpp As Short
		Dim lintCip As Short
		Dim lintCco As Short
		Dim lintCcp As Short
		
		Dim lblnExi As Boolean
		
		Dim lrstUnC As ADODB.Recordset
		lrstUnC = New ADODB.Recordset
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		'Faturas
		Dim llngAMe As Integer
		Dim lstrIte As String
		Dim lintMed As Short
		Dim lstrFas As String
		Dim ldblCon As Double
		Dim lintCus As Short
		
		Dim ldteAux As Date
		'O..S
		Dim ldteSol As Date
		Dim ldteCon As Date
		Dim ldteLoo As Date
		Dim ldblPra As Double
		Dim lstrSer As String
		Dim lintLigacoes As Short
		Dim lintLigacoesForaPrazo As Short
		Dim lintLigacoesPendentes As Short
		Dim lintReligacaoUrgencia As Short
		Dim lintReligacaoUrgenciaForaPrazo As Short
		Dim lintReligacaoNormal As Short
		Dim lintReligacaoNormalForaPrazo As Short
		
		Dim lstrProcedente As String
		
		Dim lstrCdEmpr As String
		Dim lstrCdIbge As String
		Dim lstrNmIbge As String
		
		lstrCdEmpr = LeConfig("EMPRESA_COD_RES89")
		Call Executar("DROP TABLE IF EXISTS `oficio_31_2014`")
		lstrSql = ""
		lstrSql = lstrSql & "CREATE TABLE IF NOT EXISTS `oficio_31_2014` ( "
		lstrSql = lstrSql & "  `cdempresa` varchar(4) DEFAULT '', "
		lstrSql = lstrSql & "  `anomes` int(6) DEFAULT '0', "
		lstrSql = lstrSql & "  `ibge` varchar(9) DEFAULT '', "
		lstrSql = lstrSql & "  `nomeibge` varchar(50) DEFAULT '', "
		lstrSql = lstrSql & "  `zona` varchar(3) DEFAULT '000', "
		lstrSql = lstrSql & "  `livro` varchar(2) DEFAULT '00', "
		lstrSql = lstrSql & "  `conta` varchar(6) DEFAULT '000000', "
		lstrSql = lstrSql & "  `unid_id` int(11) DEFAULT '0', "
		lstrSql = lstrSql & "  `ligados` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `media` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `custo` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `sem_fatura` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `com_fatura` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `classe_ru` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `classe_re` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `classe_in` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `classe_sp` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `classe_pp` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `classe_ip` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `classe_co` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `classe_cp` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `danos_eletricos` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `danos_eletricos_procedente` int(1) DEFAULT '0', "
		
		lstrSql = lstrSql & "  `b_rural_ligacoes` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `b_rural_ligacoes_fora_prazo` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `b_rural_ligacoes_pendentes` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `b_urbano_ligacoes` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `b_urbano_ligacoes_fora_prazo` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `b_urbano_ligacoes_pendentes` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `a_ligacoes` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `a_ligacoes_fora_prazo` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `a_ligacoes_pendentes` int(1) DEFAULT '0', "
		
		lstrSql = lstrSql & "  `b_rural_religacao_urgencia` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `b_rural_religacao_urgencia_fora_prazo` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `b_urbano_religacao_urgencia` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `b_urbano_religacao_urgencia_fora_prazo` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `a_religacao_urgencia` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `a_religacao_urgencia_fora_prazo` int(1) DEFAULT '0', "
		
		lstrSql = lstrSql & "  `b_rural_religacao_normal` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `b_rural_religacao_normal_fora_prazo` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `b_urbano_religacao_normal` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `b_urbano_religacao_normal_fora_prazo` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `a_religacao_normal` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  `a_religacao_normal_fora_prazo` int(1) DEFAULT '0', "
		lstrSql = lstrSql & "  PRIMARY KEY (`cdempresa`, `anomes`, `ibge`, `nomeibge`, `zona`, `livro`, `conta`, `unid_id`), "
		lstrSql = lstrSql & "  key `keyanomes` (`anomes`),"
		lstrSql = lstrSql & "  key `keyzona` (`zona`(3)),"
		lstrSql = lstrSql & "  key `keylivro` (`livro`(2)),"
		lstrSql = lstrSql & "  key `keyconta` (`conta`(6)),"
		lstrSql = lstrSql & "  key `keyunid` (`unid_id`)"
		lstrSql = lstrSql & ") ENGINE=InnoDB DEFAULT CHARSET=latin1 "
		Call Executar(lstrSql)
		Me.lblInfo1.Text = " Buscando as unidades consumidoras aguarde..."
		Call Me.lblInfo1.Refresh()
		lstrSql = "SELECT * FROM `munic` WHERE zona = '001' "
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .RecordCount > 0 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"'
					lstrCdIbge = IIf(IsDbNull(.Fields("ibge").value), "", .Fields("ibge").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"'
					lstrNmIbge = IIf(IsDbNull(.Fields("nome").value), "", .Fields("nome").value)
				End If
				Call .Close()
			End If
		End With
		lstrSql = ""
		lstrSql = lstrSql & " UPDATE `oficio_31_2014` SET "
		lstrSql = lstrSql & "  `b_rural_ligacoes` = 0, "
		lstrSql = lstrSql & "  `b_rural_ligacoes_fora_prazo` = 0, "
		lstrSql = lstrSql & "  `b_rural_ligacoes_pendentes` = 0, "
		lstrSql = lstrSql & "  `b_rural_religacao_urgencia` = 0, "
		lstrSql = lstrSql & "  `b_rural_religacao_urgencia_fora_prazo` = 0, "
		lstrSql = lstrSql & "  `b_rural_religacao_normal` = 0, "
		lstrSql = lstrSql & "  `b_rural_religacao_normal_fora_prazo` = 0, "
		lstrSql = lstrSql & "  `b_urbano_ligacoes` = 0, "
		lstrSql = lstrSql & "  `b_urbano_ligacoes_fora_prazo` = 0, "
		lstrSql = lstrSql & "  `b_urbano_ligacoes_pendentes` = 0, "
		lstrSql = lstrSql & "  `b_urbano_religacao_urgencia` = 0, "
		lstrSql = lstrSql & "  `b_urbano_religacao_urgencia_fora_prazo` = 0, "
		lstrSql = lstrSql & "  `b_urbano_religacao_normal` = 0, "
		lstrSql = lstrSql & "  `b_urbano_religacao_normal_fora_prazo` = 0, "
		lstrSql = lstrSql & "  `a_ligacoes` = 0, "
		lstrSql = lstrSql & "  `a_ligacoes_fora_prazo` = 0, "
		lstrSql = lstrSql & "  `a_ligacoes_pendentes` = 0, "
		lstrSql = lstrSql & "  `a_religacao_urgencia` = 0, "
		lstrSql = lstrSql & "  `a_religacao_urgencia_fora_prazo` = 0, "
		lstrSql = lstrSql & "  `a_religacao_normal` = 0, "
		lstrSql = lstrSql & "  `a_religacao_normal_fora_prazo` = 0 "
		Call Executar(lstrSql)
		
		lstrSql = ""
		lstrSql = lstrSql & "SELECT `grupo`, `regiao`, `zona`, `livro`, `conta`, `cons_id`, `unid_id`, `nome`, `data_ligacao`, `situacao`, `clas` " & vbCrLf
		lstrSql = lstrSql & "  FROM `unidb` " & vbCrLf
		lstrSql = lstrSql & " WHERE `situacao` <> 'N' " & vbCrLf
		lstrSql = lstrSql & " UNION " & vbCrLf
		lstrSql = lstrSql & "SELECT `grupo`, `regiao`, `zona`, `livro`, `conta`, `cons_id`, `unid_id`, `nome`, `data_ligacao`, `situacao`, `clas` " & vbCrLf
		lstrSql = lstrSql & "  FROM `unida` " & vbCrLf
		lstrSql = lstrSql & " WHERE `situacao` <> 'N'"
		Call Conectar(lstrSql, lrstUnC)
		With lrstUnC
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .RecordCount > 0 Then
					Call BarraProgresso((Me.ProgressBar1), (Me.lblPerc1), (Me.lblInfo1), 0, .RecordCount)
					While Not .EOF
						'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"'
						lstrGru = VB.Left(IIf(IsDbNull(.Fields("grupo").value), "B1", .Fields("grupo").value), 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"'
						lstrReg = IIf(IsDbNull(.Fields("regiao").value), "U", .Fields("regiao").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"'
						lstrZon = IIf(IsDbNull(.Fields("zona").value), "", .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"'
						lstrLiv = IIf(IsDbNull(.Fields("livro").value), "", .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"'
						lstrCon = IIf(IsDbNull(.Fields("conta").value), "", .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"'
						lstrNom = IIf(IsDbNull(.Fields("conta").value), "", .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"'
						llngUId = IIf(IsDbNull(.Fields("unid_id").value), 0, .Fields("unid_id").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"'
						llngCId = IIf(IsDbNull(.Fields("cons_id").value), 0, .Fields("cons_id").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"'
						ldteLig = IIf(IsDbNull(.Fields("data_ligacao").value), "1900-01-01", .Fields("data_ligacao").value)
						llngAMe = CInt(VB6.Format(ldteLig, "yyyyMM"))
						'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"'
						lstrCla = IIf(IsDbNull(.Fields("clas").value), "9101", .Fields("clas").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"'
						lstrSit = IIf(IsDbNull(.Fields("situacao").value), "L", .Fields("situacao").value)
						lintCru = 0
						lintCre = 0
						lintCin = 0
						lintCsp = 0
						lintCpp = 0
						lintCip = 0
						lintCco = 0
						lintCcp = 0
						Select Case CInt(VB.Left(lstrCla, 1))
							Case Is < 4
								lintCin = 1
							Case Is = 4
								lintCco = 1
							Case Is = 5
								lintCru = 1
							Case Is = 6
								If VB.Left(lstrCla, 2) = "64" Then
									lintCip = 1
								Else
									lintCpp = 1
								End If
							Case Is = 7
								lintCsp = 1
							Case Is = 8
								lintCcp = 1
							Case Else
								lintCre = 1
						End Select
						If ldteLig >= CDate("01/01/2010") And ldteLig <= CDate("31/12/2014") Then
							lblnExi = VerificaSeExiste(llngAMe, lstrZon, lstrLiv, lstrCon, llngUId)
							If lblnExi Then
								lstrSql = "UPDATE "
							Else
								lstrSql = "INSERT INTO "
							End If
							lstrSql = lstrSql & "`oficio_31_2014` SET "
							lstrSql = lstrSql & "  `cdempresa`  = '" & lstrCdEmpr & "', "
							lstrSql = lstrSql & "  `ibge`  = '" & lstrCdIbge & "', "
							lstrSql = lstrSql & "  `nomeibge`  = '" & lstrNmIbge & "', "
							lstrSql = lstrSql & "  `anomes`  =  " & llngAMe & ", "
							lstrSql = lstrSql & "  `zona`    = '" & lstrZon & "', "
							lstrSql = lstrSql & "  `livro`   = '" & lstrLiv & "', "
							lstrSql = lstrSql & "  `conta`   = '" & lstrCon & "', "
							lstrSql = lstrSql & "  `unid_id` =  " & llngUId & ", "
							lstrSql = lstrSql & "  `ligados` = 1, "
							lstrSql = lstrSql & "  `classe_ru` =  " & lintCru & ", "
							lstrSql = lstrSql & "  `classe_re` =  " & lintCre & ", "
							lstrSql = lstrSql & "  `classe_in` =  " & lintCin & ", "
							lstrSql = lstrSql & "  `classe_sp` =  " & lintCsp & ", "
							lstrSql = lstrSql & "  `classe_pp` =  " & lintCpp & ", "
							lstrSql = lstrSql & "  `classe_ip` =  " & lintCip & ", "
							lstrSql = lstrSql & "  `classe_co` =  " & lintCco & ", "
							lstrSql = lstrSql & "  `classe_cp` =  " & lintCcp & " "
							If lblnExi Then
								lstrSql = lstrSql & " WHERE `anomes` = " & llngAMe & " "
								lstrSql = lstrSql & "   AND `unid_id` = " & llngUId & " "
								lstrSql = lstrSql & "   AND `zona`  = '" & lstrZon & "' "
								lstrSql = lstrSql & "   AND `livro` = '" & lstrLiv & "' "
								lstrSql = lstrSql & "   AND `conta` = '" & lstrCon & "' "
							End If
							Call Executar(lstrSql)
						End If
						If Me.chkHistorico.CheckState = System.Windows.Forms.CheckState.Checked Then
							Me.lblInfo2.Text = " Buscando o historico das ligações da unidade consumidora aguarde..."
							Call Me.lblInfo2.Refresh()
							lstrSql = ""
							lstrSql = lstrSql & "SELECT * "
							lstrSql = lstrSql & "  FROM `hist_unid` "
							lstrSql = lstrSql & " WHERE `evento` LIKE '%NOVA%' "
							lstrSql = lstrSql & "   AND `data` BETWEEN '2010-01-01' AND '2014-12-31' "
							lstrSql = lstrSql & "   AND `zona`  = '" & lstrZon & "' "
							lstrSql = lstrSql & "   AND `livro` = '" & lstrLiv & "' "
							lstrSql = lstrSql & "   AND `conta` = '" & lstrCon & "' "
							lstrSql = lstrSql & " ORDER BY `data` ASC "
							Call Conectar(lstrSql, lrstAux)
							With lrstAux
								If .State = ADODB.ObjectStateEnum.adStateOpen Then
									If .RecordCount > 0 Then
										Call BarraProgresso((Me.ProgressBar2), (Me.lblPerc2), (Me.lblInfo2), 0, .RecordCount)
										While Not .EOF
											'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"'
											ldteAux = CDate(IIf(IsDbNull(.Fields("data").value), "1900-01-01", .Fields("data").value))
											llngAMe = CInt(VB6.Format(ldteAux, "yyyyMM"))
											If ldteAux >= CDate("01/01/2010") And ldteAux <= CDate("31/12/2014") Then
												lblnExi = VerificaSeExiste(llngAMe, lstrZon, lstrLiv, lstrCon, llngUId)
												If lblnExi Then
													lstrSql = "UPDATE "
												Else
													lstrSql = "INSERT INTO "
												End If
												
												lstrSql = lstrSql & "`oficio_31_2014` SET "
												lstrSql = lstrSql & "  `cdempresa`  = '" & lstrCdEmpr & "', "
												lstrSql = lstrSql & "  `ibge`  = '" & lstrCdIbge & "', "
												lstrSql = lstrSql & "  `nomeibge`  = '" & lstrNmIbge & "', "
												lstrSql = lstrSql & "  `anomes`  =  " & llngAMe & ", "
												lstrSql = lstrSql & "  `zona`    = '" & lstrZon & "', "
												lstrSql = lstrSql & "  `livro`   = '" & lstrLiv & "', "
												lstrSql = lstrSql & "  `conta`   = '" & lstrCon & "', "
												lstrSql = lstrSql & "  `unid_id` =  " & llngUId & ", "
												lstrSql = lstrSql & "  `ligados` = 1, "
												lstrSql = lstrSql & "  `classe_ru` =  " & lintCru & ", "
												lstrSql = lstrSql & "  `classe_re` =  " & lintCre & ", "
												lstrSql = lstrSql & "  `classe_in` =  " & lintCin & ", "
												lstrSql = lstrSql & "  `classe_sp` =  " & lintCsp & ", "
												lstrSql = lstrSql & "  `classe_pp` =  " & lintCpp & ", "
												lstrSql = lstrSql & "  `classe_ip` =  " & lintCip & ", "
												lstrSql = lstrSql & "  `classe_co` =  " & lintCco & ", "
												lstrSql = lstrSql & "  `classe_cp` =  " & lintCcp & " "
												If lblnExi Then
													lstrSql = lstrSql & " WHERE `anomes` = " & llngAMe & " "
													lstrSql = lstrSql & "   AND `unid_id` = " & llngUId & " "
													lstrSql = lstrSql & "   AND `zona`  = '" & lstrZon & "' "
													lstrSql = lstrSql & "   AND `livro` = '" & lstrLiv & "' "
													lstrSql = lstrSql & "   AND `conta` = '" & lstrCon & "' "
												End If
												Call Executar(lstrSql)
											End If
											Call .MoveNext()
											Call BarraProgresso((Me.ProgressBar2), (Me.lblPerc2), (Me.lblInfo2), 1,  , " Histórico ")
										End While
									End If
									Call .Close()
								End If
							End With
						End If
						If Me.chkFaturas.CheckState = System.Windows.Forms.CheckState.Checked Then
							Me.lblInfo2.Text = " Buscando as faturas da unidade consumidora aguarde..."
							Call Me.lblInfo2.Refresh()
							lstrSql = ""
							lstrSql = lstrSql & "SELECT * "
							lstrSql = lstrSql & "  FROM `nf` "
							lstrSql = lstrSql & " WHERE `mesano_ref` BETWEEN 201001 AND 201412 "
							lstrSql = lstrSql & "  AND `mesano` IN "
							lstrSql = lstrSql & "( "
							lstrSql = lstrSql & "'01/2010','02/2010','03/2010','04/2010','05/2010','06/2010','07/2010','08/2010','09/2010','10/2010','11/2010','12/2010', "
							lstrSql = lstrSql & "'01/2011','02/2011','03/2011','04/2011','05/2011','06/2011','07/2011','08/2011','09/2011','10/2011','11/2011','12/2011', "
							lstrSql = lstrSql & "'01/2012','02/2012','03/2012','04/2012','05/2012','06/2012','07/2012','08/2012','09/2012','10/2012','11/2012','12/2012', "
							lstrSql = lstrSql & "'01/2013','02/2013','03/2013','04/2013','05/2013','06/2013','07/2013','08/2013','09/2013','10/2013','11/2013','12/2013', "
							lstrSql = lstrSql & "'01/2014','02/2014','03/2014','04/2014','05/2014','06/2014','07/2014','08/2014','09/2014','10/2014','11/2014','12/2014' "
							lstrSql = lstrSql & ") "
							lstrSql = lstrSql & "   AND `unid_id` = " & llngUId & " "
							lstrSql = lstrSql & "   AND `zona`  = '" & lstrZon & "' "
							lstrSql = lstrSql & "   AND `livro` = '" & lstrLiv & "' "
							lstrSql = lstrSql & "   AND `conta` = '" & lstrCon & "' "
							lstrSql = lstrSql & "   AND `grupo` = '" & lstrGru & "' "
							Call Conectar(lstrSql, lrstAux)
							With lrstAux
								If .State = ADODB.ObjectStateEnum.adStateOpen Then
									If .RecordCount > 0 Then
										Call BarraProgresso((Me.ProgressBar2), (Me.lblPerc2), (Me.lblInfo2), 0, .RecordCount)
										While Not .EOF
											'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"'
											llngCId = IIf(IsDbNull(.Fields("cons_id").value), 0, .Fields("cons_id").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"'
											llngAMe = 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"'
											lstrIte = IIf(IsDbNull(.Fields("itensserv").value), "", .Fields("itensserv").value)
											lintMed = BuscaValorItem(lstrIte, "90410000")
											ldblCon = BuscaValorItem(lstrIte, "030201009")
											'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"'
											lstrFas = IIf(IsDbNull(.Fields("fase").value), "M", .Fields("fase").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"'
											lstrCla = IIf(IsDbNull(.Fields("codclas").value), "9101", .Fields("codclas").value)
											If lstrFas = "M" And ldblCon = 30 Then
												lintCus = 1
											ElseIf lstrFas = "B" And ldblCon = 50 Then 
												lintCus = 1
											ElseIf lstrFas = "M" And ldblCon = 100 Then 
												lintCus = 1
											Else
												lintCus = 0
											End If
											lintCru = 0
											lintCre = 0
											lintCin = 0
											lintCsp = 0
											lintCpp = 0
											lintCip = 0
											lintCco = 0
											lintCcp = 0
											Select Case CInt(VB.Left(lstrCla, 1))
												Case Is < 4
													lintCin = 1
												Case Is = 4
													lintCco = 1
												Case Is = 5
													lintCru = 1
												Case Is = 6
													If VB.Left(lstrCla, 2) = "64" Then
														lintCip = 1
													Else
														lintCpp = 1
													End If
												Case Is = 7
													lintCsp = 1
												Case Is = 8
													lintCcp = 1
												Case Else
													lintCre = 1
											End Select
											lblnExi = VerificaSeExiste(llngAMe, lstrZon, lstrLiv, lstrCon, llngUId)
											If lblnExi Then
												lstrSql = "UPDATE "
											Else
												lstrSql = "INSERT INTO "
											End If
											lstrSql = lstrSql & "`oficio_31_2014` SET "
											lstrSql = lstrSql & "  `cdempresa`  = '" & lstrCdEmpr & "', "
											lstrSql = lstrSql & "  `ibge`  = '" & lstrCdIbge & "', "
											lstrSql = lstrSql & "  `nomeibge`  = '" & lstrNmIbge & "', "
											lstrSql = lstrSql & "  `anomes`  =  " & llngAMe & ", "
											lstrSql = lstrSql & "  `zona`    = '" & lstrZon & "', "
											lstrSql = lstrSql & "  `livro`   = '" & lstrLiv & "', "
											lstrSql = lstrSql & "  `conta`   = '" & lstrCon & "', "
											lstrSql = lstrSql & "  `unid_id` =  " & llngUId & ", "
											lstrSql = lstrSql & "  `ligados` = 1, "
											lstrSql = lstrSql & "  `media`   =  " & lintMed & ", "
											lstrSql = lstrSql & "  `custo`   =  " & lintCus & ", "
											lstrSql = lstrSql & "  `sem_fatura` = 0, "
											lstrSql = lstrSql & "  `com_fatura` = 1, "
											lstrSql = lstrSql & "  `classe_ru` =  " & lintCru & ", "
											lstrSql = lstrSql & "  `classe_re` =  " & lintCre & ", "
											lstrSql = lstrSql & "  `classe_in` =  " & lintCin & ", "
											lstrSql = lstrSql & "  `classe_sp` =  " & lintCsp & ", "
											lstrSql = lstrSql & "  `classe_pp` =  " & lintCpp & ", "
											lstrSql = lstrSql & "  `classe_ip` =  " & lintCip & ", "
											lstrSql = lstrSql & "  `classe_co` =  " & lintCco & ", "
											lstrSql = lstrSql & "  `classe_cp` =  " & lintCcp & " "
											If lblnExi Then
												lstrSql = lstrSql & " WHERE `anomes` = " & llngAMe & " "
												lstrSql = lstrSql & "   AND `unid_id` = " & llngUId & " "
												lstrSql = lstrSql & "   AND `zona`  = '" & lstrZon & "' "
												lstrSql = lstrSql & "   AND `livro` = '" & lstrLiv & "' "
												lstrSql = lstrSql & "   AND `conta` = '" & lstrCon & "' "
											End If
											Call Executar(lstrSql)
											Call .MoveNext()
											Call BarraProgresso((Me.ProgressBar2), (Me.lblPerc2), (Me.lblInfo2), 1,  , " Faturas ")
										End While
									End If
									Call .Close()
								End If
							End With
						End If
						If Me.chkOS.CheckState = System.Windows.Forms.CheckState.Checked Then
							Me.lblInfo2.Text = " Buscando as ordens de servico da unidade consumidora aguarde..."
							Call Me.lblInfo2.Refresh()
							
							'(4257,4258,4259,4260,4261,4262,4263,4264,4265,4266,4267,4268,4269,4270,4271,4272,4273)
							'Ordem de serviço
							lstrSql = ""
							lstrSql = lstrSql & "SELECT * " & vbCrLf
							lstrSql = lstrSql & "  FROM `solserv` " & vbCrLf
							lstrSql = lstrSql & " WHERE `sit` = 'F' " & vbCrLf
							lstrSql = lstrSql & "   AND `unid_id` = " & llngUId & " " & vbCrLf
							lstrSql = lstrSql & "   AND `unid` = '" & lstrGru & "' " & vbCrLf
							lstrSql = lstrSql & "   AND (LEFT(`itensserv`, 10) = '9050000002' OR LEFT(`itensserv`, 9) IN('904000004','904000005')) " & vbCrLf
							lstrSql = lstrSql & "   AND ((`datasolu` BETWEEN '2010-01-01' AND '2014-12-31') OR " & vbCrLf
							lstrSql = lstrSql & "        (`data_soli` BETWEEN '2010-01-01' AND '2014-12-31')) " & vbCrLf
							Call Conectar(lstrSql, lrstAux)
							With lrstAux
								If .State = ADODB.ObjectStateEnum.adStateOpen Then
									If .RecordCount > 0 Then
										Call BarraProgresso((Me.ProgressBar2), (Me.lblPerc2), (Me.lblInfo2), 0, .RecordCount)
										While Not .EOF
											lintLigacoes = 0
											lintLigacoesForaPrazo = 0
											lintLigacoesPendentes = 0
											lintReligacaoUrgencia = 0
											lintReligacaoUrgenciaForaPrazo = 0
											lintReligacaoNormal = 0
											lintReligacaoNormalForaPrazo = 0
											'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"'
											lstrSer = VB.Left(IIf(IsDbNull(.Fields("itensserv").value), "", .Fields("itensserv").value), 10)
											'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"'
											ldteSol = CDate(IIf(IsDbNull(.Fields("data_soli").value), "1900-01-01", .Fields("data_soli").value) & " " & VB6.Format(IIf(IsDbNull(.Fields("hora_soli").value), "00:00:00", .Fields("hora_soli").value), "HH:NN:SS"))
											'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"'
											ldteCon = CDate(IIf(IsDbNull(.Fields("datasolu").value), "1900-01-01", .Fields("datasolu").value) & " " & VB6.Format(IIf(IsDbNull(.Fields("horasolu").value), "00:00:00", .Fields("horasolu").value), "HH:NN:SS"))
											llngAMe = CInt(VB6.Format(ldteSol, "yyyyMM"))
											'Calcula Dias
											ldteLoo = CDate(VB6.Format(ldteSol, "DD/MM/YYYY"))
											ldteAux = CDate(VB6.Format(ldteCon, "DD/MM/YYYY"))
											ldblPra = 0
											While ldteLoo <= ldteAux
												If VerificaDiaUtil(ldteLoo) Then
													ldblPra = ldblPra + 1
												End If
												ldteLoo = System.Date.FromOADate(DateSerial(Year(ldteLoo), Month(ldteLoo), VB.Day(ldteLoo)).ToOADate + 1)
											End While
											
											If VB.Left(lstrSer, 9) = "904000004" Then 'Religação Normal 24/48
												'UPGRADE_WARNING: DateDiff behavior may be different. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6B38EC3F-686D-4B2E-B5A5-9E8E7A762E32"'
												ldblPra = Arredondar(DateDiff(Microsoft.VisualBasic.DateInterval.Second, ldteSol, ldteCon, FirstDayOfWeek.Saturday, FirstWeekOfYear.Jan1) / 3600, 2)
												If lstrReg = "R" Then
													If ldblPra > 48 Then
														lintReligacaoNormalForaPrazo = 1
														lintReligacaoNormal = 0
													Else
														lintReligacaoNormalForaPrazo = 0
														lintReligacaoNormal = 1
													End If
												Else
													If ldblPra > 24 Then
														lintReligacaoNormalForaPrazo = 1
														lintReligacaoNormal = 0
													Else
														lintReligacaoNormalForaPrazo = 0
														lintReligacaoNormal = 1
													End If
												End If
											ElseIf VB.Left(lstrSer, 9) = "904000005" Then  'Religação Urgencia 4/8
												'UPGRADE_WARNING: DateDiff behavior may be different. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6B38EC3F-686D-4B2E-B5A5-9E8E7A762E32"'
												ldblPra = Arredondar(DateDiff(Microsoft.VisualBasic.DateInterval.Second, ldteSol, ldteCon, FirstDayOfWeek.Saturday, FirstWeekOfYear.Jan1) / 3600, 2)
												If lstrReg = "R" Then
													If ldblPra > 8 Then
														lintReligacaoUrgenciaForaPrazo = 1
														lintReligacaoUrgencia = 0
													Else
														lintReligacaoUrgenciaForaPrazo = 0
														lintReligacaoUrgencia = 1
													End If
												Else
													If ldblPra > 4 Then
														lintReligacaoUrgenciaForaPrazo = 1
														lintReligacaoUrgencia = 0
													Else
														lintReligacaoUrgenciaForaPrazo = 0
														lintReligacaoUrgencia = 1
													End If
												End If
											ElseIf VB.Left(lstrSer, 10) = "9050000002" Then  'Ligação Nova Marretada para nunca ultrapassar
												lintLigacoes = 1
												lintLigacoesPendentes = 0
												lintLigacoesForaPrazo = 0
											Else
												lintLigacoes = 0
												lintLigacoesPendentes = 0
												lintLigacoesForaPrazo = 0
											End If
											lblnExi = VerificaSeExiste(llngAMe, lstrZon, lstrLiv, lstrCon, llngUId)
											If lblnExi Then
												lstrSql = "UPDATE "
											Else
												lstrSql = "INSERT INTO "
											End If
											lstrSql = lstrSql & "`oficio_31_2014` SET "
											lstrSql = lstrSql & "  `cdempresa`  = '" & lstrCdEmpr & "', "
											lstrSql = lstrSql & "  `ibge`  = '" & lstrCdIbge & "', "
											lstrSql = lstrSql & "  `nomeibge`  = '" & lstrNmIbge & "', "
											lstrSql = lstrSql & "  `anomes`  =  " & llngAMe & ", "
											lstrSql = lstrSql & "  `zona`    = '" & lstrZon & "', "
											lstrSql = lstrSql & "  `livro`   = '" & lstrLiv & "', "
											lstrSql = lstrSql & "  `conta`   = '" & lstrCon & "', "
											lstrSql = lstrSql & "  `unid_id` =  " & llngUId & ", "
											lstrSql = lstrSql & "  `classe_ru` =  " & lintCru & ", "
											lstrSql = lstrSql & "  `classe_re` =  " & lintCre & ", "
											lstrSql = lstrSql & "  `classe_in` =  " & lintCin & ", "
											lstrSql = lstrSql & "  `classe_sp` =  " & lintCsp & ", "
											lstrSql = lstrSql & "  `classe_pp` =  " & lintCpp & ", "
											lstrSql = lstrSql & "  `classe_ip` =  " & lintCip & ", "
											lstrSql = lstrSql & "  `classe_co` =  " & lintCco & ", "
											lstrSql = lstrSql & "  `classe_cp` =  " & lintCcp & ", "
											
											If lstrGru = "B" Then
												If lstrReg = "R" Then
													If VB.Left(lstrSer, 10) = "9050000002" Then
														lstrSql = lstrSql & "  `b_rural_ligacoes` = " & lintLigacoes & ", "
														lstrSql = lstrSql & "  `b_rural_ligacoes_fora_prazo` = " & lintLigacoesForaPrazo & ", "
														lstrSql = lstrSql & "  `b_rural_ligacoes_pendentes` = " & lintLigacoesPendentes & " "
													End If
													If VB.Left(lstrSer, 9) = "904000005" Then
														lstrSql = lstrSql & "  `b_rural_religacao_urgencia` = " & lintReligacaoUrgencia & ", "
														lstrSql = lstrSql & "  `b_rural_religacao_urgencia_fora_prazo` = " & lintReligacaoUrgenciaForaPrazo & " "
													End If
													If VB.Left(lstrSer, 9) = "904000004" Then
														lstrSql = lstrSql & "  `b_rural_religacao_normal` = " & lintReligacaoNormal & ", "
														lstrSql = lstrSql & "  `b_rural_religacao_normal_fora_prazo` = " & lintReligacaoNormalForaPrazo & " "
													End If
												Else
													If VB.Left(lstrSer, 10) = "9050000002" Then
														lstrSql = lstrSql & "  `b_urbano_ligacoes` = " & lintLigacoes & ", "
														lstrSql = lstrSql & "  `b_urbano_ligacoes_fora_prazo` = " & lintLigacoesForaPrazo & ", "
														lstrSql = lstrSql & "  `b_urbano_ligacoes_pendentes` = " & lintLigacoesPendentes & " "
													End If
													If VB.Left(lstrSer, 9) = "904000005" Then
														lstrSql = lstrSql & "  `b_urbano_religacao_urgencia` = " & lintReligacaoUrgencia & ", "
														lstrSql = lstrSql & "  `b_urbano_religacao_urgencia_fora_prazo` = " & lintReligacaoUrgenciaForaPrazo & " "
													End If
													If VB.Left(lstrSer, 9) = "904000004" Then
														lstrSql = lstrSql & "  `b_urbano_religacao_normal` = " & lintReligacaoNormal & ", "
														lstrSql = lstrSql & "  `b_urbano_religacao_normal_fora_prazo` = " & lintReligacaoNormalForaPrazo & " "
													End If
												End If
											Else
												If VB.Left(lstrSer, 10) = "9050000002" Then
													lstrSql = lstrSql & "  `a_ligacoes` = " & lintLigacoes & ", "
													lstrSql = lstrSql & "  `a_ligacoes_fora_prazo` = " & lintLigacoesForaPrazo & ", "
													lstrSql = lstrSql & "  `a_ligacoes_pendentes` = " & lintLigacoesPendentes & " "
												End If
												If VB.Left(lstrSer, 9) = "904000005" Then
													lstrSql = lstrSql & "  `a_religacao_urgencia` = " & lintReligacaoUrgencia & ", "
													lstrSql = lstrSql & "  `a_religacao_urgencia_fora_prazo` = " & lintReligacaoUrgenciaForaPrazo & " "
												End If
												If VB.Left(lstrSer, 9) = "904000004" Then
													lstrSql = lstrSql & "  `a_religacao_normal` = " & lintReligacaoNormal & ", "
													lstrSql = lstrSql & "  `a_religacao_normal_fora_prazo` = " & lintReligacaoNormalForaPrazo & " "
												End If
											End If
											If lblnExi Then
												lstrSql = lstrSql & " WHERE `anomes` = " & llngAMe & " "
												lstrSql = lstrSql & "   AND `unid_id` = " & llngUId & " "
												lstrSql = lstrSql & "   AND `zona`  = '" & lstrZon & "' "
												lstrSql = lstrSql & "   AND `livro` = '" & lstrLiv & "' "
												lstrSql = lstrSql & "   AND `conta` = '" & lstrCon & "' "
											End If
											Call Executar(lstrSql)
											Call .MoveNext()
											Call BarraProgresso((Me.ProgressBar2), (Me.lblPerc2), (Me.lblInfo2), 1,  , " OS ")
										End While
									End If
									Call .Close()
								End If
							End With
						End If
						If Me.chkReclamacao.CheckState = System.Windows.Forms.CheckState.Checked Then
							Me.lblInfo2.Text = " Buscando os danos eletricos da unidade consumidora aguarde..."
							Call Me.lblInfo2.Refresh()
							lstrSql = ""
							lstrSql = lstrSql & "SELECT * "
							lstrSql = lstrSql & "  FROM `reclamacao` "
							lstrSql = lstrSql & " WHERE `resolvida` = 1 "
							lstrSql = lstrSql & "   AND `unid_id` = " & llngUId & " "
							lstrSql = lstrSql & "   AND `unid` = '" & lstrGru & "' "
							lstrSql = lstrSql & "   AND `tipo_reclamacao` = 3  "
							lstrSql = lstrSql & "   AND ((`dt_solicitacao` BETWEEN '2010-01-01' AND '2014-12-31') OR "
							lstrSql = lstrSql & "        (`dt_conclusao` BETWEEN '2010-01-01' AND '2014-12-31')) "
							Call Conectar(lstrSql, lrstAux)
							With lrstAux
								If .State = ADODB.ObjectStateEnum.adStateOpen Then
									If .RecordCount > 0 Then
										Call BarraProgresso((Me.ProgressBar2), (Me.lblPerc2), (Me.lblInfo2), 0, .RecordCount)
										While Not .EOF
											'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"'
											lstrProcedente = IIf(IsDbNull(.Fields("procedente").value), "N", .Fields("procedente").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"'
											ldteSol = IIf(IsDbNull(.Fields("dt_solicitacao").value), "1900-01-01", .Fields("dt_solicitacao").value)
											llngAMe = CInt(VB6.Format(ldteSol, "yyyyMM"))
											lblnExi = VerificaSeExiste(llngAMe, lstrZon, lstrLiv, lstrCon, llngUId)
											If lblnExi Then
												lstrSql = "UPDATE "
											Else
												lstrSql = "INSERT INTO "
											End If
											lstrSql = lstrSql & "`oficio_31_2014` SET "
											lstrSql = lstrSql & "  `cdempresa`  = '" & lstrCdEmpr & "', "
											lstrSql = lstrSql & "  `ibge`  = '" & lstrCdIbge & "', "
											lstrSql = lstrSql & "  `nomeibge`  = '" & lstrNmIbge & "', "
											lstrSql = lstrSql & "  `anomes`  =  " & llngAMe & ", "
											lstrSql = lstrSql & "  `zona`    = '" & lstrZon & "', "
											lstrSql = lstrSql & "  `livro`   = '" & lstrLiv & "', "
											lstrSql = lstrSql & "  `conta`   = '" & lstrCon & "', "
											lstrSql = lstrSql & "  `unid_id` =  " & llngUId & ", "
											lstrSql = lstrSql & "  `classe_ru` =  " & lintCru & ", "
											lstrSql = lstrSql & "  `classe_re` =  " & lintCre & ", "
											lstrSql = lstrSql & "  `classe_in` =  " & lintCin & ", "
											lstrSql = lstrSql & "  `classe_sp` =  " & lintCsp & ", "
											lstrSql = lstrSql & "  `classe_pp` =  " & lintCpp & ", "
											lstrSql = lstrSql & "  `classe_ip` =  " & lintCip & ", "
											lstrSql = lstrSql & "  `classe_co` =  " & lintCco & ", "
											lstrSql = lstrSql & "  `classe_cp` =  " & lintCcp & ", "
											lstrSql = lstrSql & "  `danos_eletricos` = 1, "
											lstrSql = lstrSql & "  `danos_eletricos_procedente` = " & IIf(lstrProcedente = "S", 1, 0) & " "
											If lblnExi Then
												lstrSql = lstrSql & " WHERE `anomes` = " & llngAMe & " "
												lstrSql = lstrSql & "   AND `unid_id` = " & llngUId & " "
												lstrSql = lstrSql & "   AND `zona`  = '" & lstrZon & "' "
												lstrSql = lstrSql & "   AND `livro` = '" & lstrLiv & "' "
												lstrSql = lstrSql & "   AND `conta` = '" & lstrCon & "' "
											End If
											Call Executar(lstrSql)
											Call .MoveNext()
											Call BarraProgresso((Me.ProgressBar1), (Me.lblPerc1), (Me.lblInfo1), 1,  , " D.E. ")
										End While
									End If
									Call .Close()
								End If
							End With
						End If
						Call .MoveNext()
						Call BarraProgresso((Me.ProgressBar1), (Me.lblPerc1), (Me.lblInfo1), 1,  , " U.C. " & VB6.Format(ldteLig, "dd/MM/yyyy") & " " & llngUId & " ")
					End While
				End If
				Call .Close()
			End If
		End With
		'UPGRADE_NOTE: Object lrstUnC 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"'
		lrstUnC = Nothing
		'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
		Call MsgBox("OK")
	End Sub
	
	Private Sub frmGerarOficio312014_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Me.txtArquivoFaturamento.Text = My.Application.Info.DirectoryPath & "\" & gstrEmpresa_Sigla & "_faturamento.txt"
		Me.txtArquivoClasse.Text = My.Application.Info.DirectoryPath & "\" & gstrEmpresa_Sigla & "_classe.txt"
		Me.txtArquivoRessarcimento.Text = My.Application.Info.DirectoryPath & "\" & gstrEmpresa_Sigla & "_ressarcimento.txt"
		Me.txtArquivoLigacao.Text = My.Application.Info.DirectoryPath & "\" & gstrEmpresa_Sigla & "_ligacao.txt"
		Me.txtArquivoReligacao.Text = My.Application.Info.DirectoryPath & "\" & gstrEmpresa_Sigla & "_religacao.txt"
	End Sub
End Class