Error code Overflow and wildcard not working Excel VBA - overflow

In the following code, I get an error at line pec1 = pec+1, in which is indicated to be some 30000 number. pec6, pec3, pec5 all also have numbers greater than 1000.
Also looking a little further down, when I do a wildcard search 72-41-* for the numbers like 72-41-00, 72-41-13, it gives a return value of 0.
Thank you so much for helping me.
*update: Please help me. Excel now stops running after I try to run the program. :(
Sheets("Sheet1").Select
Dim egr As String, pec1 As Long, pec2 As Long, pec3 As Long
Dim pec4 As Long
Dim pec5 As Long
Dim pec6 As Long
Dim lastroww As Long
Dim k As Long
Dim h As Long
Dim om As String
egr = ""
pec1 = 0
pec2 = 0
pec3 = 0
pec4 = 0
pec5 = 0
pec6 = 0
dummy = 0
k = 4
With Sheet1
.AutoFilterMode = False
.Range("A3:K3").AutoFilter
.Range("A3:K3").AutoFilter Field:=3, Criteria1:="05.00 PEC"
End With
With ActiveSheet
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
lastroww = lastrow - 3
Do Until Cells(k, 4).Value = ""
For k = 4 To lastroww
pm = Cells(k, 8).Value
If pm = "" Then
pec6 = pec6 + 1
ElseIf pm = "Semais,June" Then
pec1 = pec1 + 1
ElseIf pm = "Mwier,Robert" Then
pec3 = pec3 + 1
ElseIf pm = "Newton,Sally" Then
pec5 = pec5 + 1
End If
om = Cells(k, 7).Value
If om = "" Then
ElseIf om = "72-41*" Then
pec2 = pec2 + 1
ElseIf om = "72-51*" Or om = "72-52-*" Or om = "72-53-*" Then
pec4 = pec4 + 1
End If
Next k
Loop

Related

How Export fpspread to Excel vb6?

I try to export to Excel with the fpsread plugin, but there really is no information on how, I have searched the manual but they only show me how to do it with .net
Will someone have an idea?
I managed to do it was very simple, but wanting to import the titles was the heaviest, but here I leave the code in case any work
Private Sub CmdImportar_Click()
Call Export_Excel(cdgExcel, sprFacturas)
Call HacerBusqueda
End Sub
Public Sub Export_Excel(cdgExcel As CommonDialog, Spread As fpSpread)
Dim Header() As String
Dim I As Integer
Dim j As Integer
Dim x As Integer
With cdgExcel
.CancelError = False
.InitDir = "C:/:"
.Filter = "Excel(*.xls)|*.xls"
.ShowSave
If .filename <> "" Then
Spread.Redraw = False
For I = 1 To Spread.ColHeaderRows
ReDim Header(Spread.MaxCols) As String
Spread.Row = SpreadHeader + (I - 1)
For j = 1 To Spread.MaxCols
Spread.Col = j
Header(j) = Spread.Text & ""
Next j
Spread.MaxRows = Spread.MaxRows + 1
Spread.Row = I
Spread.Action = ActionInsertRow
For j = 1 To Spread.MaxCols
Spread.Col = j
Spread.CellType = Spread.CellType
Spread.TypeHAlign = Spread.TypeHAlign
Spread.TypeVAlign = Spread.TypeVAlign
Spread.Text = Header(j) & ""
Next j
Next I
x = Spread.ExportToExcel(.filename, "Sheet1", "")
For I = 1 To Spread.ColHeaderRows
Spread.Row = 1
Spread.Action = ActionDeleteRow
Next I
If x = True Then
MsgBox .filename & vbNewLine & "Se ha Importado el archivo", vbInformation, "Resultado"
Else
MsgBox "No se ha podido exportar el archivo", vbCritical, "Error"
End If
End If
End With
End Sub

Import XML Data thru MSFlexigrid to SQL Table using vb6

My VB6 code reads XML file
loads into MSFLEXIGRID
loops through each record, if I see a break point,
does not add data in sql table except one record why it is happening does not know. does not throw any error.
I'm appending my code below:
Private Sub cmdPrint_Click()
Dim rsMtrData As New ADODB.Recordset
Dim irow As Integer
rsClose rsMtrData
rsMtrData.Open "select * from Master_Meter_Reading ", cn, adOpenKeyset, adLockPessimistic
With MSFlexGrid1
rsMtrData.AddNew
For irow = 0 To .Rows - 1
.Row = irow
.Col = 0
rsMtrData!Snapshot = .text & ""
.Col = 1
rsMtrData!LoadSurveyDTime = Format(.text, "dd-MM-yyyy hh:mm:SS")
.Col = 2
rsMtrData!LoadSurveyDateDay = g_ConvertNumber(.text)
.Col = 3
rsMtrData!LoadSurveyDate = Format(.text, g_DateFormat)
.Col = 4
rsMtrData!LoadSurveyDateParmVal = g_ConvertNumber(.text)
.Col = 5
rsMtrData!LoadSurveyType = .text & ""
.Col = 6
rsMtrData!LoadSurveyUnit = g_ConvertNumber(.text)
.Col = 7
rsMtrData!LoadSurveySlipValParmVal = .text & ""
.Col = 8
rsMtrData!LoadSurveySlipValParmValTime = g_ConvertNumber(.text)
rsMtrData.Update
Next irow
End With
MsgBox "Data Successfully Saved", vbInformation
End Sub
You are only adding one record then updating it. At the moment the AddNew is on the outside of the loop. If you change the code and move the AddNew inside the For..Next loop it should add more rows.
Your code would look something like this:
Private Sub cmdPrint_Click()
Dim rsMtrData As New ADODB.Recordset
Dim irow As Integer
rsClose rsMtrData
rsMtrData.Open "select * from Master_Meter_Reading ", cn, adOpenKeyset, adLockPessimistic
With MSFlexGrid1
For irow = 0 To .Rows - 1
rsMtrData.AddNew 'This is the line that has moved from outside to inside the loop.
.Row = irow
.Col = 0
rsMtrData!Snapshot = .text & ""
.Col = 1
rsMtrData!LoadSurveyDTime = Format(.text, "dd-MM-yyyy hh:mm:SS")
.Col = 2
rsMtrData!LoadSurveyDateDay = g_ConvertNumber(.text)
.Col = 3
rsMtrData!LoadSurveyDate = Format(.text, g_DateFormat)
.Col = 4
rsMtrData!LoadSurveyDateParmVal = g_ConvertNumber(.text)
.Col = 5
rsMtrData!LoadSurveyType = .text & ""
.Col = 6
rsMtrData!LoadSurveyUnit = g_ConvertNumber(.text)
.Col = 7
rsMtrData!LoadSurveySlipValParmVal = .text & ""
.Col = 8
rsMtrData!LoadSurveySlipValParmValTime = g_ConvertNumber(.text)
rsMtrData.Update
Next irow
End With
MsgBox "Data Successfully Saved", vbInformation
End Sub

DateDiff between years resulting wrong result

I got this function from the internet. It's doing well when counting days so far, but when the dates is from different year, the result is wrong.
Example:
dateFrom = "2017-12-26"
dateTo = "2018-01-02"
the result will be 28 days, when it supposed to be 6 days.
Here is the function:
Public Function NetWorkdays(dtStartDate, dtEndDate, arrHolidays)
Dim lngDays
Dim lngSaturdays
Dim lngSundays
Dim lngHolidays
Dim lngAdjustment
Dim dtTest
Dim i, x
lngDays = DateDiff("d", dtStartDate, dtEndDate)
lngSundays = DateDiff("ww", dtStartDate, dtEndDate, vbSunday)
lngSaturdays = DateDiff("w", IIf(Weekday(dtStartDate, vbSunday) = vbSaturday, dtStartDate, dtStartDate - Weekday(dtStartDate, vbSunday)), dtEndDate)
For x = LBound(arrHolidays) To UBound(arrHolidays)
For i = 0 To lngDays
dtTest = DateAdd("d", i, dtStartDate)
If arrHolidays(x) = dtTest And Weekday(dtTest) <> 1 And Weekday(dtTest) <> 7 Then
lngHolidays = lngHolidays + 1
End If
Next
Next
If Weekday(dtStartDate, vbSunday) = vbSunday Or Weekday(dtStartDate, vbSunday) = vbSaturday Then
lngAdjustment = 0
Else
lngAdjustment = 1
End If
NetWorkdays = lngDays - lngSundays - lngSaturdays - lngHolidays + lngAdjustment
End Function
Public Function IIf(expr, truepart, falsepart)
If expr Then IIf = truepart Else IIf = falsepart
End function
Can anybody point it out anything to repair?
dateFrom = #2017-12-26#
dateTo = #2018-01-02#
Msgbox Dateto - datefrom,, "Result"
returns
---------------------------
Result
---------------------------
7
---------------------------
OK
---------------------------
As indiated by the type prefixes in the prototype:
Public Function NetWorkdays(dtStartDate, dtEndDate, arrHolidays)
the function expects Dates, not Strings. Evidence:
Option Explicit
(copy of function)
Dim dp, n
For Each dp In Array(Array("2017-12-26", "2018-01-02"))
On Error Resume Next
n = NetWorkdays(dp(0), dp(1), Array())
If Err Then n = Err.Description
On Error GoTo 0
WScript.Echo TypeName(dp(0)), dp(0), dp(1), n
dp(0) = CDate(dp(0))
dp(1) = CDate(dp(1))
WScript.Echo TypeName(dp(0)), dp(0), dp(1), NetWorkdays(dp(0), dp(1), Array())
Next
output (german locale):
cscript 47921079.vbs
String 2017-12-26 2018-01-02 Typenkonflikt
Date 26.12.2017 02.01.2018 6
Depending on versions, locales and the phase of the moon, you may have to replace the CDate() call with something more reliable.

Crystal report not showing in application

I have a function that i am using to show crystal reports in my application. Everything was fine until yesterday afternoon and now it is showing nothing but a blank window. But it is not giving any error.In crystal report designer it is showing values while previewing. I am using stored procedure to retrieve values from DB. Here is my code
Public Sub ShowReport(ParamArray reportParameters())
On Error GoTo Catch
Dim NTOT As Integer
Dim nCtr As Integer
Dim LoopCount As Integer
Dim ReportPath As String
Open App.Path & "/Reports.txt" For Input As #1
Input #1, ReportPath
Close #1
ReportPath = ReportPath & "\Reports\" & reportParameters(0)
'MsgBox ReportPath
Screen.MousePointer = vbHourglass
With frmReports.Crpt
.Reset
.WindowTop = 0
.WindowLeft = 0
.ReportFileName = ReportPath
'.RetrieveStoredProcParams
For LoopCount = 3 To UBound(reportParameters)
.StoredProcParam(LoopCount - 3) = reportParameters(LoopCount)
Next
.WindowTitle = reportParameters(1)
.ReportTitle = reportParameters(1)
.WindowParentHandle = frmReports.hwnd
.WindowShowSearchBtn = True
.WindowShowPrintSetupBtn = True
.WindowShowRefreshBtn = True
.WindowShowProgressCtls = True
.WindowShowZoomCtl = True
.WindowShowGroupTree = True
.WindowAllowDrillDown = True
.ProgressDialog = True
.PageZoom (100)
.WindowState = crptMaximized
If reportParameters(2) = "P" Then
.Destination = crptToPrinter
Else
.Destination = crptToWindow
End If
.Action = 1
End With
Screen.MousePointer = vbNormal
Exit Sub
Catch:
Screen.MousePointer = vbNormal
End Sub
I am using VB6 and crystal reports version is 8
what is wrong in this code? Can anyone find a solution for this

Read Data from csv file using VB

This is the code i wrote in order to First open a csv file as excel, then find the required three columns, n then read data from them n save the data into another variables showing them in textbox. As about the csv file, it contains many columns out of which my focus is on only 3 columns under title ID, L, Lg.
Problem is Excel doesnt actually open but Excel.exe process runs in task manager.
But by this point its not the compile error; Compile error comes at 'Next' Statement. It says Compile Error: Next without For!!!!
I am Confused with this one. Please help me with this one, Thanks in Advance.
Private Sub cmdFind_Click()
Dim xlApp As Excel.Application
Set xlApp = New Excel.Application
Dim X As Double, Y As Double, FleetID As String
Dim F As String, FCol As Integer, LCol As Integer, LgCol As Integer, Srno As Integer, I As Integer
Dim xlWbook As Workbook
Dim xlSht As Excel.Worksheet
Set xlWbook = xlApp.Workbooks.Open("C:\Users\saurabhvyas\Desktop\test VB2\testfile.csv")
xlApp.Visible = True
Set xlSht = xlWbook.Worksheets("sheet1")
For I = 1 To 8 Step 1
If xlSht.Cells(I, 1).Value = "ID" Then
FCol = I
Else
If xlSht.Cells(I, 1).Value = "L" Then
LCol = I
Else
If xlSht.Cells(I, 1).Value = "Lg" Then
LgCol = I
End If
Next I
Set Srno = 2
Do
If xlSht.Cells(FCol, Srno).Value = Str$(txtF.Text) Then
Set X = xlSht.Cells(LCol, Srno).Value
Set Y = xlSht.Cells(LgCol, Srno).Value
End If
Srno = Srno + 1
Loop While xlSht.Cells(FCol, Srno).Value = vbNullString
txtL.Text = Str$(X)
txtLg.Text = Str$(Y)
xlWbook.Close
xlApp.Quit
Excel.Application.Close
Set xlSht = Nothing
Set xlWbook = Nothing
Set xlApp = Nothing
End Sub
You can open CSV format text files and operate on them using ADO with the Jet Provider's Text IISAM. Much less clunky than automating Excel. Or you can read the lines as text and Split() them on commas.
What you're doing does open Excel, but you haven't asked Excel to be visible... though I have no idea why you'd want that.
What are you really trying to do?
As for your compile error, that's because you are missing some End Ifs.
Write it as:
For I = 1 To 8 Step 1
If xlSht.Cells(I, 1).Value = "ID" Then
FCol = I
Else
If xlSht.Cells(I, 1).Value = "L" Then
LCol = I
Else
If xlSht.Cells(I, 1).Value = "Lg" Then
LgCol = I
End If
End If
End If
Next I
Or as:
For I = 1 To 8 Step 1
If xlSht.Cells(I, 1).Value = "ID" Then
FCol = I
ElseIf xlSht.Cells(I, 1).Value = "L" Then
LCol = I
ElseIf xlSht.Cells(I, 1).Value = "Lg" Then
LgCol = I
End If
Next I

Resources