I have a strange bug where my currency field adds $1 to every entry. I have the values stored in a table, and use a lookup to get those values
ReducedLunch = DLookup("[Cost]", "LunchCost", "[ID]=2")
NormalLunch = DLookup("[Cost]", "LunchCost", "[ID]=1")
Milk = DLookup("[Cost]", "LunchCost", "[ID]=4")
NoLunch = DLookup("[Cost]", "LunchCost", "[ID]=5")
Then I have some VB code to write the appropriate value to the database field...
If (rs!TodaysLunch = "Lunch" And rs!FreeLunch = False And rs!ReducedLunch = False) Then
DailyCost = NormalLunch
rs.Edit
rs!TodaysCost = DailyCost
rs!Balance = rs!Balance - DailyCost
rs.Update
End If
If I put a msgbox command before the Endif to show the DailyCost value it shows the correct value at this point, however when I actually write the data back to the table
DoCmd.RunSQL "INSERT INTO Lunch (StudentID, DateOfLunch, TypeOfLunch, Cost) SELECT [ID],[TodaysDate],[TodaysLunch],[TodaysCost] FROM Students"
TodaysCost has added an extra dollar. I've checked and double checked everything, but can't find where it's getting this mystery value from. I tried setting DailyCost to zero (it's defined as currency), but it still writes back the value + 1.
Here is my complete code and the msgbox command near the end is at what point a 1 is added...
Dim DailyCost As Currency
Dim ReducedLunch, NormalLunch, Milk, NoLunch As Variant
Dim rs As Recordset
Dim db As Database
DailyCost = 0
Set db = CurrentDb()
Set rs = db.OpenRecordset("SELECT * FROM Students")
ReducedLunch = DLookup("[Cost]", "LunchCost", "[ID]=2")
NormalLunch = DLookup("[Cost]", "LunchCost", "[ID]=1")
Milk = DLookup("[Cost]", "LunchCost", "[ID]=4")
NoLunch = DLookup("[Cost]", "LunchCost", "[ID]=5")
Do Until rs.EOF = True
'Check for free or reduced and get price
If (rs!TodaysLunch = "Lunch" And rs!FreeLunch = True) Then
DailyCost = 0
rs.Edit
rs!TodaysCost = DailyCost
rs!Balance = rs!Balance - DailyCost
rs.Update
End If
If (rs!TodaysLunch = "Lunch" And rs!ReducedLunch = True) Then
DailyCost = ReducedLunch
rs.Edit
rs!TodaysCost = DailyCost
rs!Balance = rs!Balance - DailyCost
rs.Update
End If
If (rs!TodaysLunch = "Lunch" And rs!FreeLunch = False And rs!ReducedLunch = False) Then
DailyCost = NormalLunch
rs.Edit
rs!TodaysCost = DailyCost
rs!Balance = rs!Balance - DailyCost
rs.Update
End If
If (rs!TodaysLunch = "Milk") Then
DailyCost = Milk
rs.Edit
rs!TodaysCost = DailyCost
rs!Balance = rs!Balance - DailyCost
rs.Update
End If
If (rs!TodaysLunch = "Lunch XtraMilk") Then
ElseIf (rs!ReducedLunch = True) Then
DailyCost = ReducedLunch + Milk * 2
ElseIf (rs!FreeLunch = True) Then
DailyCost = FreeLunch + Milk * 2
ElseIf (rs!FreeLunch = False And rs!ReducedLunch = False) Then
DailyCost = NormalLunch + Milk * 2
rs.Edit
rs!TodaysCost = DailyCost
rs!Balance = rs!Balance - DailyCost
rs.Update
End If
If (rs!TodaysLunch = "No Lunch") Then
DailyCost = 0
rs.Edit
rs!TodaysCost = DailyCost
rs!Balance = rs!Balance - DailyCost
rs.Update
End If
'Set date to today
rs.Edit
rs!TodaysDate = Date
MsgBox (rs!TodaysCost) 'Point where 1 is added
rs.Update
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
DoCmd.RunSQL "INSERT INTO Lunch (StudentID, DateOfLunch, TypeOfLunch, Cost) SELECT [ID],[TodaysDate],[TodaysLunch],[TodaysCost] FROM Students"
Make sure that TodaysCost is not Long Integer.
Related
After getting more information here, I think there is need to validate the data before storing in a stagnant table in Ms access. I’m not sure about this topic frankly its new to me , I’m just try to see whether it will work.
I want to check whether Set rs = db.OpenRecordset("tblEfdReceipts") has data before proceeding to update , below is the full code:
Set rs = db.OpenRecordset("tblEfdReceipts")
If lngStatus > 0 Then
ElseIf lngStatus < 0 Then
' Handle error.
On Error Resume Next
End If
' Process data.
Set JSONS = JsonConverter.ParseJson(strData)
Z = 2
For Each item In JSONS
With rs
.AddNew
rs![TPIN] = item("TPIN")
rs![TaxpayerName] = item("TaxpayerName")
rs![Address] = item("Address")
rs![ESDTime] = item("ESDTime")
rs![TerminalID] = item("TerminalID")
rs![InvoiceCode] = item("InvoiceCode")
rs![InvoiceNumber] = item("InvoiceCode")
rs![FiscalCode] = item("FiscalCode")
rs![TalkTime] = item("TalkTime")
rs![Operator] = item("Operator")
rs![Taxlabel] = item("TaxItems")("TaxLabel")
rs![CategoryName] = item("TaxItems")("CategoryName")
rs![Rate] = item("TaxItems")("Rate")
rs![TaxAmount] = item("TaxItems")("TaxAmount")
rs![VerificationUrl] = item("TaxItems")("VerificationUrl")
rs![INVID] = Me.InvoiceID
rs.Update
End With
Z = Z + 1
Next
rs.Close
Set rs = Nothing
Set db = Nothing
Set JSONS = Nothing
I’m trying this code but I’m not sure of how do it correctly, the received data is never part of the live until checked.
Validation code require improvements
rs = Me.Recordset.Clone
If Me.Recordset.RecordCount = 0 then 'checks for number of records
msgbox "There is no records"
End if
If the data is present then process it.
Use DCount:
If DCount("*", "tblEfdReceipts") = 0 Then
' Table has no records.
Set rs = db.OpenRecordset("tblEfdReceipts")
' <snip>
Else
' Table has records. Skip.
End if
As a general rule, you can check EOF (end of file).
Thus:
Set rs = db.OpenRecordset("tblEfdReceipts")
if rs.EOF = True then
' no records
End If
I am trying to fill 5 sub forms(Fr_Rep_x)(x=1 to 5) in a report(Report1) with recordset values(5 different pages) by invoking a public function 5 times.
The subforms and the public function are invoked in the report_activate event
I am getting the above error 2465: Application-defined or object-defined error
on the below line of the public function:
Reports("Report1").fr_name.Controls(Lbl_ON).Caption = rst("Order_Number")
Report Activate Event
Private Sub Report_Activate()
Dim i As Integer
For i = 1 To 5
DoCmd.OpenForm "Fr_Rep_" & i, , , , , acHidden
Call MORep(i, Forms!Fr_Main!NavSubform.Form!Lbox_Comp.Column(0), "00" & i & "0", Forms!Fr_Main!NavSubform.Form!Lbox_Comp.Column(3))
Next i
End Sub
Public Function
Public Sub MORep(Form_num As Integer, MO_ON As Long, MO_Op As String, Mat As String)
Dim fr_name As String
fr_name = "Fr_Rep_" & Form_num
var_order = MO_ON
var_op_n = MO_Op
var_mat = Mat
var_matdes = DLookup("Material_Description", "Tbl_MO_Rev_Control", "Material = '" & var_mat & "'")
var_matrev = DLookup("MO_Rev_No", "Tbl_MO_List_Archive", "Order_Number = " & var_order)
Dim rst As DAO.Recordset
Dim dbs As DAO.Database
Set dbs = CurrentDb()
Set rst = dbs.OpenRecordset("Tbl_MO_List_Archive", Type:=dbOpenDynaset)
rst.FindFirst "[Order_Number] = " & var_order '& "*'" & "AND MO_Rev_No ='" & var_mo_rev & ""
Dim str1
Reports("Report1").fr_name.Controls(Lbl_ON).Caption = rst("Order_Number")
Reports("Report1").fr_name.Form.Lbl_OQ.Caption = rst("Order_Qty")
Reports("Report1").fr_name.Form.Lbl_Batch.Caption = rst("Batch")
Reports("Report1").fr_name.Form.Lbl_MRP.Caption = rst("MRP_Controller")
Reports("Report1").fr_name.Form.Lbl_PC.Caption = rst("Profit_Center")
Reports("Report1").fr_name.Form.Lbl_PS.Caption = rst("Production_Scheduler")
Reports("Report1").fr_name.Form.Lbl_Mat.Caption = rst("Material")
Reports("Report1").fr_name.Form.Lbl_Pr_D.Caption = Format(rst("MO_Issue_Date"), "DD-MMM-YYYY")
Reports("Report1").fr_name.Form.Lbl_Pr_T.Caption = rst("MO_Issue_Time")
Set rst = Nothing
Forms(fr_name).Controls(Lbl_Mat_D).Caption = var_matdes
Forms(fr_name).Controls(Lbl_T_Pg).Caption = DLookup("Total_Pages", "Tbl_MO_Rev_Control", "MO_Rev_No = '" & var_matrev & "'")
Forms(fr_name).Controls(Lbl_SAP).Caption = "Form # " & DLookup("SAP_Form_no", "Tbl_MO_Rev_Control", "MO_Rev_No = '" & var_matrev & "'")
Set rst = dbs.OpenRecordset("Tbl_MO_Op_Archive", Type:=dbOpenDynaset)
rst.FindFirst "[Order_Number] = " & var_order '& "*'" & "AND MO_Rev_No ='" & var_mo_rev & ""
If var_op_n = "0010" Then
Forms(fr_name).Controls(Lbl_Ver_Drw_T).Visible = True
Forms(fr_name).Controls(Lbl_Ver_Drw_B).Visible = True
Forms(fr_name).Controls(Lbl_Ver_Drw_Sign).Visible = True
Forms(fr_name).Controls(Lbl_Ver_Drw_Dt).Visible = True
Forms(fr_name).Controls(Lbl_Pg).Caption = rst("10_Page_No")
Forms(fr_name).Controls(Lbl_Ver_Drw_Sign).Caption = rst("10_Verif_Drw_Rev_Stamp")
Forms(fr_name).Controls(Lbl_Ver_Drw_Dt).Caption = rst("10_Verif_Drw_Rev_Date_Stamp")
'hide stamp labels for op 0020 to 0050
Forms(fr_name).Controls(Lbl_FPV).Visible = False
Forms(fr_name).Controls(Lbl_Clk).Visible = False
Forms(fr_name).Controls(Lbl_Sign_Date).Visible = False
Forms(fr_name).Controls(Lbl_VS_Acc).Visible = False
Forms(fr_name).Controls(Lbl_VS_Rej).Visible = False
Forms(fr_name).Controls(Lbl_PC_NA).Visible = False
Forms(fr_name).Controls(Img_VS_Stamp).Visible = False
Forms(fr_name).Controls(Lbl_Stamp_Type).Visible = False
Forms(fr_name).Controls(Lbl_PC_Stamp).Visible = False
Forms(fr_name).Controls(Lbl_PC_Acc).Visible = False
Forms(fr_name).Controls(Lbl_PC_Rej).Visible = False
Forms(fr_name).Controls(Lbl_Rej).Visible = False
ElseIf var_op_n = "0020" Then
Forms(fr_name).Controls(Lbl_FPV).Visible = True
Forms(fr_name).Controls(Lbl_Clk).Visible = True
Forms(fr_name).Controls(Lbl_Sign_Date).Visible = True
Forms(fr_name).Controls(Lbl_VS_Acc).Visible = True
Forms(fr_name).Controls(Lbl_VS_Rej).Visible = True
Forms(fr_name).Controls(Lbl_PC_NA).Visible = True
Forms(fr_name).Controls(Lbl_PC_Acc).Visible = True
Forms(fr_name).Controls(Lbl_PC_Rej).Visible = True
Forms(fr_name).Controls(Lbl_Rej).Visible = True
Forms(fr_name).Controls(Lbl_Ver_Drw_T).Visible = False
Forms(fr_name).Controls(Lbl_Ver_Drw_B).Visible = False
Forms(fr_name).Controls(Lbl_Ver_Drw_Sign).Visible = False
Forms(fr_name).Controls(Lbl_Ver_Drw_Dt).Visible = False
Forms(fr_name).Controls(Lbl_Pg).Caption = rst("20_Page_No")
Forms(fr_name).Controls(Lbl_FPV).Caption = rst("20_First_Pc_Verif_Stamp")
Forms(fr_name).Controls(Lbl_Clk).Caption = rst("20_Clock_No_Stamp")
Forms(fr_name).Controls(Lbl_Sign_Date).Caption = rst("20_Date_Stamp")
Forms(fr_name).Controls(Lbl_VS_Acc).Caption = rst("20_Sample_Verif_Acc_Stamp")
Forms(fr_name).Controls(Lbl_VS_Rej).Caption = rst("20_Sample_Verif_Rej_Stamp")
Forms(fr_name).Controls(Lbl_PC_NA).Caption = rst("20_Verif_Stamp_Type") 'user stamp not stored in 0020 op, only NA comment
Forms(fr_name).Controls(Lbl_PC_Acc).Caption = rst("20_Pc_Comp_Acc_Stamp")
Forms(fr_name).Controls(Lbl_PC_Rej).Caption = rst("20_Pc_Comp_Rej_Stamp")
Forms(fr_name).Controls(Lbl_Rej).Caption = rst("20_Reject_No_Stamp")
ElseIf var_op_n = "0030" Then
Forms(fr_name).Controls(Img_VS_Stamp).Visible = False
Forms(fr_name).Controls(Lbl_Stamp_Type).Visible = False
Forms(fr_name).Controls(Lbl_PC_Stamp).Visible = False
Forms(fr_name).Controls(Lbl_Pg).Caption = rst("30_Page_No")
Forms(fr_name).Controls(Lbl_FPV).Caption = rst("30_First_Pc_Verif_Stamp")
Forms(fr_name).Controls(Lbl_Clk).Caption = rst("30_Clock_No_Stamp")
Forms(fr_name).Controls(Lbl_Sign_Date).Caption = rst("30_Date_Stamp")
Forms(fr_name).Controls(Lbl_VS_Acc).Caption = rst("30_Sample_Verif_Acc_Stamp")
Forms(fr_name).Controls(Lbl_VS_Rej).Caption = rst("30_Sample_Verif_Rej_Stamp")
Forms(fr_name).Controls(Lbl_PC_NA).Caption = rst("30_Verif_Stamp_Type") 'user stamp not stored in 0030 op, only NA comment
Forms(fr_name).Controls(Lbl_PC_Acc).Caption = rst("30_Pc_Comp_Acc_Stamp")
Forms(fr_name).Controls(Lbl_PC_Rej).Caption = rst("30_Pc_Comp_Rej_Stamp")
Forms(fr_name).Controls(Lbl_Rej).Caption = rst("30_Reject_No_Stamp")
ElseIf var_op_n = "0040" Then
Forms(fr_name).Controls(Lbl_Pg).Caption = rst("40_Page_No")
Forms(fr_name).Controls(Lbl_FPV).Caption = rst("40_First_Pc_Verif_Stamp")
Forms(fr_name).Controls(Lbl_Clk).Caption = rst("40_Clock_No_Stamp")
Forms(fr_name).Controls(Lbl_Sign_Date).Caption = rst("40_Date_Stamp")
Forms(fr_name).Controls(Lbl_VS_Acc).Caption = rst("40_Sample_Verif_Acc_Stamp")
Forms(fr_name).Controls(Lbl_VS_Rej).Caption = rst("40_Sample_Verif_Rej_Stamp")
Forms(fr_name).Controls(Lbl_PC_Stamp).Caption = rst("40_Verification_Stamp") 'user stamp IS stored in 0040 op
Forms(fr_name).Controls(Lbl_Stamp_Type).Caption = rst("40_Verif_Stamp_Type") 'user stamp IS stored in 0040 op
Forms(fr_name).Controls(Lbl_PC_Acc).Caption = rst("40_Pc_Comp_Acc_Stamp")
Forms(fr_name).Controls(Lbl_PC_Rej).Caption = rst("40_Pc_Comp_Rej_Stamp")
Forms(fr_name).Controls(Lbl_Rej).Caption = rst("40_Reject_No_Stamp")
ElseIf var_op_n = "0050" Then
Forms(fr_name).Controls(Img_VS_Stamp).Visible = False
Forms(fr_name).Controls(Lbl_Stamp_Type).Visible = False
Forms(fr_name).Controls(Lbl_PC_Stamp).Visible = False
Forms(fr_name).Controls(Lbl_Pg).Caption = rst("50_Page_No")
Forms(fr_name).Controls(Lbl_FPV).Caption = rst("50_First_Pc_Verif_Stamp")
Forms(fr_name).Controls(Lbl_Clk).Caption = rst("50_Clock_No_Stamp")
Forms(fr_name).Controls(Lbl_Sign_Date).Caption = rst("50_Date_Stamp")
Forms(fr_name).Controls(Lbl_VS_Acc).Caption = rst("50_Sample_Verif_Acc_Stamp")
Forms(fr_name).Controls(Lbl_VS_Rej).Caption = rst("50_Sample_Verif_Rej_Stamp")
Forms(fr_name).Controls(Lbl_PC_NA).Caption = rst("50_Verif_Stamp_Type") 'user stamp not stored in 0030 op, only NA comment
Forms(fr_name).Controls(Lbl_PC_Acc).Caption = rst("50_Pc_Comp_Acc_Stamp")
Forms(fr_name).Controls(Lbl_PC_Rej).Caption = rst("50_Pc_Comp_Rej_Stamp")
Forms(fr_name).Controls(Lbl_Rej).Caption = rst("50_Reject_No_Stamp")
End If
If Form_num = 5 Then
Set rst = Nothing
Set dbs = Nothing
End If
If var_op_n = "0020" Or var_op_n = "0050" Then
Forms(fr_name).Controls(Lbl_FPV).Top = 5250
Forms(fr_name).Controls(Lbl_Clk).Vertical = False
Forms(fr_name).Controls(Lbl_Clk).Top = 5250
Forms(fr_name).Controls(Lbl_Sign_Date).Top = 5250
Forms(fr_name).Controls(Lbl_VS_Acc).Top = 5250
Forms(fr_name).Controls(Lbl_VS_Rej).Top = 5250
Forms(fr_name).Controls(Lbl_PC_NA).Top = 5250
Forms(fr_name).Controls(Lbl_PC_Acc).Top = 5250
Forms(fr_name).Controls(Lbl_PC_Rej).Top = 5250
Forms(fr_name).Controls(Lbl_Rej).Top = 5250
Else
Forms(fr_name).Controls(Lbl_FPV).Top = 7250
Forms(fr_name).Controls(Lbl_Clk).Vertical = True
Forms(fr_name).Controls(Lbl_Clk).Top = 7250
Forms(fr_name).Controls(Lbl_Sign_Date).Top = 7250
Forms(fr_name).Controls(Lbl_VS_Acc).Top = 7250
Forms(fr_name).Controls(Lbl_VS_Rej).Top = 7250
Forms(fr_name).Controls(Lbl_PC_NA).Top = 7250
Forms(fr_name).Controls(Lbl_PC_Acc).Top = 7250
Forms(fr_name).Controls(Lbl_PC_Rej).Top = 7250
Forms(fr_name).Controls(Lbl_Rej).Top = 7250
End If
Call OpNum
Call MatNum
Call MatRev
'Forms(fr_name)(Lbox_Op_List).Requery
'Forms(fr_name).Controls(Lbox_Op).Requery
Forms(fr_name).Controls(Lbl_Op).Caption = DLookup("Operation_Description", "Tbl_Ops_Desc", "MO_Rev_No = '" & var_matrev & "' AND Oper_No ='" & var_op_n & "'")
End Sub
Report Structure and Code movement Getting stuck at step 6, All forms have same controls and control names
please advice, I guess the report subform control referencing is wrong?
As to your code, Lbl_ON has not been assigned a value here:
Reports("Report1").fr_name.Controls(Lbl_ON).Caption = rst("Order_Number")
To check, insert a debug line. Also, try specifying the Value property:
Debug.Print "Lbl_ON:", Lbl_ON
Reports("Report1").fr_name.Controls(Lbl_ON).Caption = rst("Order_Number").Value
If Lbl_ON can be Null, there is no control to access, thus:
If Not IsNull(Lbl_ON) then
Reports("Report1").fr_name.Controls(Lbl_ON).Caption = rst("Order_Number").Value
End If
That will, of course, not adjust anything, so you will wish to correct that Lbl_ON has no value.
Im trying to pull data from 2 different tables in one button click event. I've checked over everything and doesn't seem to be any typo's or anything but keep getting this error.
Below is my code for the button click event
Protected Sub btnFindRepair_Click(sender As Object, e As EventArgs) Handles btnFindRepair.Click
Dim connection As New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\ITrepair.mdf;Integrated Security=True")
Dim command As New SqlCommand("SELECT * from Repair; SELECT * FROM Customer WHERE Tracking_Number = #Tracking_Number", connection)
command.Parameters.Add("#Tracking_Number", SqlDbType.Int).Value = txtTrackingNumber.Text
Dim adapter As New SqlDataAdapter(command)
Dim ds As System.Data.DataSet
Dim table As New DataTable()
adapter.Fill(table)
'Repair Details
DDLBookedInBy.SelectedItem.Text = ""
DDLDeviceType.SelectedItem.Text = ""
txtBookedInDate.Text = ""
txtDeviceName.Text = ""
DDLAccessories.SelectedItem.Text = ""
txtDevicePassword.Text = ""
DDLRepairType.Text = ""
txtTechnical.Text = ""
txtCompletedNotes.Text = ""
DDLRepairStatus.Text = ""
'Customer Details
txtFname.Text = ""
txtLname.Text = ""
txtContactNum.Text = ""
txtAltContactNum.Text = ""
txtAddress.Text = ""
If table.Rows.Count() > 0 Then
' return only 1 row
DDLBookedInBy.SelectedItem.Text = ds.tables(0).Rows(0)(2).ToString()
DDLDeviceType.SelectedItem.Text = ds.tables(0).Rows(0)(3).ToString()
txtBookedInDate.Text = ds.tables(0).Rows(0)(4).ToString()
txtDeviceName.Text = ds.tables(0).Rows(0)(5).ToString()
DDLAccessories.SelectedItem.Text = ds.tables(0).Rows(0)(6).ToString()
txtDevicePassword.Text = ds.tables(0).Rows(0)(7).ToString()
DDLRepairType.Text = ds.tables(0).Rows(0)(8).ToString()
txtTechnical.Text = ds.tables(0).Rows(0)(9).ToString()
txtCompletedNotes.Text = ds.tables(0).Rows(0)(10).ToString()
txtFname.Text = ds.tables(1).Rows(1)(4).ToString()
txtLname.Text = table.Rows(1)(5).ToString()
txtContactNum.Text = table.Rows(1)(6).ToString()
txtAltContactNum.Text = table.Rows(1)(7).ToString()
txtAddress.Text = table.Rows(1)(8).ToString()
Else
MsgBox("NO DATA found")
End If
End Sub
Replace all occurences of ds.tables(0) with table. You haven't initialized the DataSet ds but you don't need it anyway because you fill the DataTable tbl with adapter.Fill(table).
For example:
If table.Rows.Count > 0 Then
DDLBookedInBy.SelectedItem.Text = table.Rows(0)(2).ToString()
' .... '
If you want to fill the DataSet use:
Dim ds As System.Data.DataSet
Dim table As New DataTable()
ds = New DataSet()
adapter.Fill(ds)
If table.Rows.Count > 0 Then
DDLBookedInBy.SelectedItem.Text = ds.Tables(0).Rows(0)(2).ToString()
' .... '
txtFname.Text = ds.Tables(1).Rows(1)(4).ToString()
' ... '
I am getting a "Method 'Range' of object '_Global' failed" error about 50% of the time I try to run the below code. Debug takes me to this line:
Set rng = xlWS.Range(Range("A1"), xlWS.Range("A1").SpecialCells(xlLastCell))
Can anyone help with this problem?? Thanks.
Private Sub Command48_Click()
'On Error Resume Next
Dim Filename As String
Dim month1 As String
Dim year1 As Integer
Dim startTime As Date
startTime = Now
Dim strDirectoryPath As String
Filename = strDirectoryPath & "\" & "QI_GAP_REPORT_2_ " & Format$(Now(), "mm-dd-yyyy") & ".xls"
DoCmd.OpenQuery "QI_GAP_REPORT_FOR_EXCEL"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "QI_GAP_REPORT_FOR_EXCEL", Filename, False, "Summary"
DoCmd.Close acQuery, "QI_GAP_REPORT_FOR_EXCEL"
'///****Format excel workbook****////
' Late binding to avoid reference:
Dim xlApp As Object 'Excel.Application
Dim xlWB As Object 'Workbook
Dim xlWS As Object 'Worksheet
Dim GetBook As String
' Create the instance of Excel that we will use to open the temp book
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlWB = xlApp.Workbooks.Open(Filename)
Set xlWS = xlWB.Worksheets("Summary")
' Format our temp sheet
' ************************************************** *************************
xlApp.Range("A1").Select
Const xlLandscape As Long = 2
Const xlCenter As Long = -4108
Const xlBottom As Long = -4107
Const xlContext As Integer = -5002
Const xlDown As Integer = -4121
Const xlContinuous As Integer = 1
Const xlThin As Integer = 2
Const xlLastCell As Long = 11
Const xlYes As Long = 1
With xlWS
With .UsedRange
.borders.LineStyle = xlContinuous
.borders.ColorIndex = 0
.borders.TintAndShade = 0
.borders.Weight = xlThin
End With
'format header 90 degree
With .Range("i1:y1")
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 90
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
.UsedRange.Rows.RowHeight = 15
.UsedRange.Columns.AutoFit
Dim tbl As ListObject
Dim rng As Range
Set rng = xlWS.Range(Range("A1"), xlWS.Range("A1").SpecialCells(xlLastCell))
Set tbl = ActiveSheet.ListObjects.Add(xlSrcRange, rng, , xlYes)
tbl.TableStyle = "TableStyleMedium2"
tbl.ShowTotals = True
I got a form, which has 2 buttons, 1 is to set the input data file, 1 is to start a sub, the sub should make a query against the db. But the same simple query "select * from opt_in_customer_record;" return different thing! That's horrible! Why???
Here is my code, btnBrowse_Click() will pop window for user to select file, everytime I will the same file. btnGenData_Click() is the sub which got problem.
for the data file, here is the first 20 lines, Event_Plan_Code is the first column.
5BUDP;HongKong;050111;520010100000800
5BUDP;HongKong;010111;520010100100867
5BUDP;HongKong;130111;520010100182001
3BUDP;HongKong;050111;520010100244746
5BUDP;HongKong;040111;520010100282676
1BUDP;HongKong;110111;520010100310573
1BUDP;HongKong;120111;520010100310573
3BUDP;HongKong;310111;520010100361924
1BUDP;HongKong;310111;520010100392644
1BUDP;HongKong;290111;520010100406914
3BUDP;HongKong;280111;520010100429143
3BUDP;HongKong;190111;520010100440403
3BUDP;HongKong;300111;520010100482444
1BUDP;HongKong;130111;520010100523409
3BUDP;HongKong;210111;520010100576847
5BUDP;HongKong;230111;520010100583232
3BUDP;HongKong;200111;520010100637103
3BUDP;HongKong;160111;520010100639083
3BUDP;HongKong;190111;520010100666157
3BUDP;HongKong;250111;520010100774408
I made the program to stop if the first character of Event_Plan_Code is 1, just to stop the program for debugging. And each time I press the button, different result I got:
1st run:
5BUDP
5BUDP
5BUDP
3BUDP
5BUDP
1BUDP
it make sense.
2nd run:
3BUDP
1BUDP
The problem is that the query should start over again and the result should be the same! Now different result I got.
Thank you very much if you may answer my question!
Option Compare Database
Private Sub btnBrowse_Click()
Dim filePath As String
filePath = LaunchCD(Me)
txtFilePath.Value = filePath
txtStatus.Value = ""
End Sub
Private Sub btnGenData_Click()
'On Error GoTo Error_Handling
Dim extractCdrFlag As Boolean
txtStatus.Value = ""
If IsNull(txtFilePath.Value) Then
MsgBox "Please enter a valid input file location."
Else
txtStatus.Value = ""
txtStatus.Value = txtStatus.Value & "Deleting previous record from table Opt_In_Customer_Record..." & vbCrLf
CurrentDb.Execute "deleteAll"
txtStatus.Value = txtStatus.Value & "Delete successfully." & vbCrLf
If FileExists(txtFilePath.Value) Then
txtStatus.Value = txtStatus.Value & "Trying to import data from file..." & vbCrLf
DoCmd.TransferText acImportDelim, "Import_Specification", "Opt_In_Customer_Record", txtFilePath.Value, False
txtStatus.Value = txtStatus.Value & "Data imported successfully." & vbCrLf
Testing
txtStatus.Value = ""
Else
MsgBox "File does not exist. Please enter again."
End If
End If
Exit Sub
Error_Handling:
MsgBox "Error while generating data! Please check your data setting!"
Exit Sub
End Sub
Sub Testing()
'On Error GoTo Error_Handling
Dim conConnection As New ADODB.Connection
Dim cmdCommand As New ADODB.Command
Dim rstRecordSet As New ADODB.Recordset
Dim eventPlanCode As String
Dim visitedCountry As String
Dim startDateTxt As String
Dim startDate As Date
Dim endDate As Date
Dim imsi As String
Dim currentMonth As String
Dim nextMonth As String
Dim currentYear As String
Dim nextYear As String
Dim temp As Integer
Dim sql As String
'MsgBox CurrentDb.Name
With conConnection
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = CurrentDb.Name
.Open
End With
'MsgBox conConnection.ConnectionString
With cmdCommand
.ActiveConnection = conConnection
.CommandText = "SELECT * FROM Opt_In_Customer_Record;"
.CommandType = adCmdText
End With
With rstRecordSet
.CursorType = adOpenStatic
.CursorLocation = adUseClient
.LockType = adLockOptimistic
.Open cmdCommand
End With
If rstRecordSet.EOF = False Then
rstRecordSet.MoveFirst
Do
'Debug.Print txtStatus.Value
eventPlanCode = rstRecordSet!Event_Plan_Code
visitedCountry = rstRecordSet!Visited_Country
startDateTxt = rstRecordSet!Start_Date
imsi = rstRecordSet!imsi
currentMonth = Mid$(startDateTxt, 3, 2) '01
currentYear = "20" & Mid$(startDateTxt, 5, 2) '2011
startDate = DateSerial(Val(currentYear), Val(currentMonth), Val(Mid$(startDateTxt, 1, 2)))
endDate = startDate + Val(Mid$(eventPlanCode, 1, 1))
MsgBox rstRecordSet!Event_Plan_Code
If (Mid$(eventPlanCode, 1, 1) = "1") Then
Exit Sub
End If
'MsgBox startDate & " " & endDate
If (currentMonth = "01") Then
nextMonth = "02"
ElseIf (currentMonth = "02") Then
nextMonth = "03"
ElseIf (currentMonth = "03") Then
nextMonth = "04"
ElseIf (currentMonth = "04") Then
nextMonth = "05"
ElseIf (currentMonth = "05") Then
nextMonth = "06"
ElseIf (currentMonth = "06") Then
nextMonth = "07"
ElseIf (currentMonth = "07") Then
nextMonth = "08"
ElseIf (currentMonth = "08") Then
nextMonth = "09"
ElseIf (currentMonth = "09") Then
nextMonth = "10"
ElseIf (currentMonth = "10") Then
nextMonth = "11"
ElseIf (currentMonth = "11") Then
nextMonth = "12"
ElseIf (currentMonth = "12") Then
nextMonth = "01"
End If
temp = Val(currentYear)
temp = temp + 1
nextYear = Str(temp)
'MsgBox currentYear & currentMonth & " " & nextYear & nextMonth
'Exit Do
rstRecordSet.MoveNext
Loop Until rstRecordSet.EOF = True
End If
'sql = "select * from ( select * from " & "dbo.inbound_rated_all_" & currentYear & currentMonth & " A inner join Opt_In_Customer_Record B "
conConnection.Close
Set conConnection = Nothing
Set cmdCommand = Nothing
Set rstRecordSet = Nothing
Exit Sub
Error_Handling:
MsgBox "Error during function Testing!"
Set conConnection = Nothing
Set cmdCommand = Nothing
Set rstRecordSet = Nothing
Exit Sub
End Sub
If you want the rows in a particular order, add an ORDER BY clause to your query:
select * from opt_in_customer_record order by event_plan_code
Actually, event_plan_code isn't the right column because it contains duplicates, but that should point you in the right direction.