Use AddDays function in vb.net - vb.net

Need some help finishing up this program, everything works and runs like I want it but I need to display an inputbox that allows the user to enter in the date they want for their invoice stored in service_date and then this date will display in the listbox with all the other items that I have put in there. I know I need to use the AddDays function but I have no clue on how to do it, and researching online has just led me to 100 other things that aren't that.
So here is my code:
Dim Customer As String
Dim Phone As String
Dim Hours As Double
Dim Parts As Double
Dim due_date As String
Dim service_date As String
Private Sub cmdInputBox_Click()
Dim service_date = InputBox("Enter the date of service. (MM/DD?YYYY)")
MsgBox("That's your date, " & service_date.ToString)
Exit Sub
End Sub
Private Sub CustInfo_Click()
Customer = txtCustomer.Text
Phone = mtbPhone.Text
Double.TryParse(txtHours.Text, Hours)
Double.TryParse(txtParts.Text, Parts)
If Customer.Length < 0 Then
MessageBox.Show("Please enter customer information.")
End If
If Phone = "" Then
MessageBox.Show("Please enter phone number.")
End If
If Not Double.TryParse(txtHours.Text, Hours) Then
MessageBox.Show("Please enter labor hours.")
End If
If Not Double.TryParse(txtParts.Text, Parts) Then
MessageBox.Show("Please enter parts and supplies.")
End If
''Perform calculations
Dim Total_Cost As Double
Dim Labor_Cost As Double
Dim Parts_Cost As Double
Parts_Cost = (Parts * 0.5 * 2)
Labor_Cost = (Hours * 35)
Total_Cost = (Hours + Parts)
Customer = txtCustomer.Text
Phone = mtbPhone.Text
lstBill.Items.Clear()
lstBill.Items.Add("Customer: " & vbTab & Customer.ToUpper)
lstBill.Items.Add("Phone: " & vbTab & vbTab & Phone)
lstBill.Items.Add("Service Date: " & vbTab & due_date)
lstBill.Items.Add("Invoice Date: " & vbTab & service_date)
lstBill.Items.Add("Labor Cost: " & vbTab & FormatCurrency(Labor_Cost))
lstBill.Items.Add("Parts Cost: " & vbTab & FormatCurrency(Parts_Cost))
lstBill.Items.Add("Total Cost: " & vbTab & FormatCurrency(Total_Cost))
Exit Sub
End Sub
Private Sub btnBill_Click(sender As System.Object, e As System.EventArgs) Handles btnBill.Click
cmdInputBox_Click()
CustInfo_Click()
End Sub

Try this:
Dim strDate As String = InputBox("Enter date?", , "")
If strDate = "" Then Exit Sub
Dim dteDate As Date
Dim enUS As New System.Globalization.CultureInfo("en-US")
If Date.TryParseExact(strDate, "MM/dd/yyyy", enUS, Globalization.DateTimeStyles.AssumeLocal, dteDate) Then
MsgBox("Date is " & dteDate.ToString)
End If

Related

Cannot display number values

I just joined and hope to learn all I can here and contribute where I can.
I am having major issues with the last three sections of my VBA script.
The correct, incorrect, and percentage score values are not being displayed on slides 40 & 41.
On slide 42 I cannot get the textbox or the label to display the username, date and their overall percentage score.
Any help on slide 40 would be great and I can workout the rest.
**Sub shapeTextHappySmile()**strong text**
Sub ShapeTextSadSmile()
Sub CertificateBuld()**
Option Explicit
Dim UserName As String
Dim numberCorrect As Integer
Dim numberIncorrect As Integer
Dim numberPercentage As Integer
Dim numberTotal As Integer
Private Sub CertDate()
Dim Rdate As Variant
Rdate = Date
Rdate = Format((Date), "mmmm dd, yyyy")
End Sub
Sub Initialise()
numberCorrect = 12
numberIncorrect = 8
numberPercentage = 58
numberTotal = 20
numberTotal = (numberCorrect + numberIncorrect)
numberCorrect = (numberTotal - numberIncorrect)
numberIncorrect = (numberTotal - numberCorrect)
numberPercentage = Round(numberCorrect / numberTotal) * 100
End Sub
Sub TakeQuiz()
UserName = InputBox(Prompt:="Type Your Name! ")
MsgBox "Welcome To The Academic Online Tutorial Quiz " + UserName, vbApplicationModal, " Academic Online Tutorial Quiz"
ActivePresentation.SlideShowWindow.View.Next
End Sub
Sub Correct()
numberCorrect = numberCorrect + 1
MsgBox ("Great well Done! That's the correct answer")
ActivePresentation.SlideShowWindow.View.Next
End Sub
Sub Incorrect()
numberIncorrect = numberIncorrect + 1
MsgBox ("Sorry! That was the incorrect answer")
ActivePresentation.SlideShowWindow.View.Next
End Sub
Sub shapeTextHappySmile()
ActivePresentation.Slides(40).Shapes(Label1).TextFrame.TextRange.Text = 12
'numberCorrect
ActivePresentation.Slides(40).Shapes(Label2).TextFrame.TextRange.Text = numberPercentage & "%"
MsgBox "Great Job, Well done " + "," & "Please print a copy of your completion certificate"
MsgBox "After printing or saving a copy of your certificate, you can exit the presentation"
With SlideShowWindows(1).View
.GotoSlide 42
End With
End Sub
Sub ShapeTextSadSmile()
ActivePresentation.Slides(41).Shapes("AnsweredIncorrectly").TextFrame.TextRange.Text = numberIncorrect
ActivePresentation.Slides(41).Shapes("InCorrectPercentage").TextFrame.TextRange.Text = numberPercentage & " %"
MsgBox "Your score was below 70%, in order to pass the quiz and receive a certificate of completion you need to score 70% or more."
MsgBox "Please retake the quiz, and good luck"
With SlideShowWindows(1).View
.GotoSlide 1
End With
' I will add the option of redoing the entire presentation or just the quiz.
'see slide 19 action buttons
End Sub
Sub CertificateBuld()
MsgBox "Great Job, Well done " + "," & "Plese print a copy of your completion certificate"
MsgBox "After printing or saving a copy of your certificate, please exit the presentation"
If numberCorrect >= "14" Then
ActivePresentation.Slides(42).Shapes(" ABCDEFGHIJKLMN ").TextFrame.TextRange.Text = " ABCDEFGHIJKLMN "
ActivePresentation.Slides(42).Shapes("Rdate & Percentage").TextFrame.TextRange.Text = " ON " & Rdate & " WITH A SCORE OF " & numberPercentage & " %"
ActivePresentation.Slides(42).Shapes(UserName).TextFrame.TextRange.Text = UserName
'OR
If numberCorrect <= "14" Then
ActivePresentation.Slides(42).Shapes(8).TextFrame.TextRange.Text = ABCDEFGHIJKLMN "
ActivePresentation.Slides(42).Shapes(9).TextFrame.TextRange.Text = Rdate & " ON " & Rdate & " WITH A SCORE OF " & numberPercentage & " %"
ActivePresentation.Slides(42).Shapes(10).TextFrame.TextRange.Text = UserName
Else
ActivePresentation.SlideShowWindow.View.Save
ActivePresentation.SlideShowWindow.View.Exit
End If
End Sub
See comments inline:
Sub shapeTextHappySmile()
' This won't work:
'ActivePresentation.Slides(40).Shapes(Label1).TextFrame.TextRange.Text = 12
' Shapes have names that are strings, so you need to use .Shapes("Label1")
' Assuming this is an ActiveX label, you get at its properties a bit
' differently from regular PPT shapes, starting with:
' .Shapes("Label1").OLEFormat.Object
' And for a Label ActiveX control, the property you want is .Caption
' And finally, Text/Caption properties take a String value so you want to
' put the 12 in quotes or convert a numeric value to string using Cstr(x)
' Final version:
ActivePresentation.Slides(40).Shapes("Label1").OLEFormat.Object.Caption = "12"
'numberCorrect
' And make the same changes to this one:
ActivePresentation.Slides(40).Shapes(Label2).TextFrame.TextRange.Text = numberPercentage & "%"
' MsgBox "Great Job, Well done " + "," & "Please print a copy of your completion certificate"
' and I think you probably want to do this instead of the above:
MsgBox "Great Job, Well done" & ", " & "Please print a copy of your completion certificate"
MsgBox "After printing or saving a copy of your certificate, you can exit the presentation"
With SlideShowWindows(1).View
.GotoSlide 42
End With
End Sub

BC30420 'Sub Main' was not found error in a Windows Form app

I've created a Windows Form application. It is my understanding that you do not have to have a Sub Main() in a Windows Form app. However I'm getting this error when I build my project:
BC30420 'Sub Main' was not found in 'LoanCalculator.Module1'.
First of all I don't know why it's saying 'LoanCalculator.Module1'. Both my form and my class are named LoanCalculator.vb. When I started the project I started writing the code in the original module. Then I added a module, named it 'LoanCalculator' and moved what code I had written to that module and finished it there. I deleted the original module. Now it builds fine with the exception of this one error. Here's my code:
Imports System.Windows.Forms
Public Class LoanCalculator
Private Sub Calculate()
Dim str As String
Dim intLoanAmt As Integer
Dim intDown As Integer
Dim intFees As Integer
Dim intBalance As Integer
Dim dblIntsRate As Single
Dim intLoanTerm As Integer
Dim sngInterestPaid As Single
Dim intTermMonths As Integer
Dim dblMonthlyPmt As Integer
Dim intTotalPaid As Integer
Dim dblYon As Double
Dim dblXon As Double
Dim dblZon As Double
If Not CheckInput() Then
Return
End If
intLoanAmt = Convert.ToInt32(txtLoan.Text)
intFees = Convert.ToInt32(txtFees.Text)
intDown = Convert.ToInt32(txtDown.Text)
intBalance = Convert.ToInt32(intLoanAmt - intDown + intFees)
intLoanTerm = Convert.ToInt32(txtTerm.Text)
dblIntsRate = Convert.ToDouble(txtTerm.Text)
intTermMonths = intLoanTerm * 12
dblYon = dblIntsRate / 1200
dblXon = dblYon + 1
dblZon = Math.Pow(dblXon, intTermMonths) - 1
dblMonthlyPmt = (dblYon + (dblYon / dblZon)) * intBalance
intTotalPaid = dblMonthlyPmt * intTermMonths
sngInterestPaid = intTotalPaid - intBalance
str = "Loan balance =" & Space(11) & intBalance.ToString & vbCrLf
str = str & "Loan Term =" & Space(16) & intLoanTerm.ToString & " years" & vbCrLf
str = str & "Interest paid =" & Space(17) & intTotalPaid.ToString & vbCrLf
str = str & "Monthly payment =" & Space(5) & dblMonthlyPmt.ToString
lblResults.Text = str
End Sub
Private Function CheckInput() As Boolean
Dim strErr As String = ""
If txtLoan.Text.Length = 0 Then
strErr = "Enter loan amount" & vbCrLf
End If
If txtDown.Text.Length = 0 Then
strErr = strErr & "Enter down payment" & vbCrLf
End If
If txtInterest.Text.Length = 0 Then
strErr = strErr & "Enter interest rate" & vbCrLf
End If
If txtFees.Text.Length = 0 Then
strErr = strErr & "Enter fees" & vbCrLf
End If
If txtTerm.Text.Length = 0 Then
strErr = strErr & "Enter loan term" & vbCrLf
End If
If strErr.Length > 0 Then
MessageBox.Show(strErr)
Return False
Else
Return True
End If
End Function
End Class
How can I fix this?

textbox date format excel vba

I have a code that allows me to manually enter date in textbox1 which then gets selected in the calendar on the useform. There is a second textbox that allows me to add or subtract dates. The code works perfectly.
Userform Code -
Option Explicit
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If IsDate(Me.TextBox1.Value) Then Me.Calendar1.Value = Me.TextBox1.Value
End Sub
Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim dt As Date
With Me
If IsDate(.TextBox1.Value) Then
dt = CDate(.TextBox1.Value) + Val(.TextBox2.Value)
.TextBox1.Value = dt
.Calendar1.Value = dt
End If
End With
End Sub
I would like to manually enter date in textbox1 in a specific format.
The formats will be -
dd
ddmmm
ddmmmyyy
I'm not sure how to write a code that does this.
The idea is to enter date in either of the 3 formats specified above in textbox1, which then gets selected on the calendar on the userform.
edited after op's clarification about allowed formats
you could build upon the following code
Option Explicit
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim txt As String, dayStr As String, monthStr As String, yearStr As String
Dim okTxt As Boolean
txt = Me.TextBox1.Value
Select Case Len(txt)
Case 2
dayStr = txt
okTxt = okDay(dayStr)
monthStr = month(Now)
yearStr = year(Now)
Case 5
dayStr = Mid(txt, 3, 3)
monthStr = Mid(txt, 3, 3)
okTxt = okDay(Left(txt, 2)) And okMonth(monthStr)
yearStr = year(Now)
Case 7
dayStr = Mid(txt, 3, 3)
monthStr = Mid(txt, 3, 3)
yearStr = Mid(txt, 6, 2)
okTxt = okDay(Left(txt, 2)) And okMonth(monthStr) And okYear(yearStr)
End Select
If Not okTxt Then
MsgBox "Invalid date" _
& vbCrLf & vbCrLf & "Date must be input in one of the following formats:" _
& vbCrLf & vbTab & "dd" _
& vbCrLf & vbTab & "ddmmm" _
& vbCrLf & vbTab & "ddmmmyy" _
& vbCrLf & vbCrLf & "Please try again", vbCritical
Cancel = True
Else
Me.Calendar1.Value = CDate(Left(txt, 2) & " " & monthStr & " " & yearStr)
End If
End Sub
Function okDay(txt As String) As Boolean
okDay = CInt(txt) > 0 And CInt(txt) < 31
End Function
Function okMonth(txt As String) As Boolean
Const months As String = "JANFEBMARAPRMAJJUNJULAUGSEPOCTNOVDEC"
okMonth = InStr(months, UCase(txt)) > 0
End Function
Function okYear(txt As String) As Boolean
okYear = CInt(txt) > 0 And CInt(txt) < 200 '<--| set your "limit" years
End Function

VBA DO Loops Issue

I am trying to create a pop up question in powerpoint VBA, so far so good. But below code doesn’t seem to work. Idea is that you get a popup box with value to enter between 100 - 200 (inclusive). But must enter a value between or can accept failed as input. The inputbox cannot be cancelled or null/empty responses. The inner loop (loop 1) seems to work ok, but if I enter 150 it doesn't terminate the loop 2 instead keeps going unless type failed but it stops with any text rather than only "failed".
Sub OnSlideShowPageChange(ByVal SSW As SlideShowWindow)
'Declare Variables
Dim xType, xLimitHi, xLimitLo, xPrompt As String
Dim InputvarTemp As String
Dim msgResult As Integer
xLimitHi = 200
xLimitLo = 100
xPrompt = "Enter Value between 100 and 200 (Inclusive)"
Do 'loop 2 check within limit or failed
msgResult = vbNo
Do 'loop 1 check Empty / Null or Cancelled input
InputvarTemp = InputBox(xPrompt, xPrompt)
If StrPtr(InputvarTemp) = 0 Then ' Check if cancelled is pressed
MsgBox "Invalid Input - Cannot be cancelled", 16, "Invalid Input."
Else
If Len(InputvarTemp) = 0 Then ' Check Null response
MsgBox "Invalid Input - Cannot be Empty / Null ", 16, "Invalid Input."
Else
msgResult = MsgBox("You have Entered " & InputvarTemp, vbYesNo + vbDefaultButton2, "Check Value in between " & xLimitLo & " to " & xLimitHi & "(Inclusive)")
If CDec(InputvarTemp) < 100 Or CDec(InputvarTemp) > 200 Then ' Check within Limits
MsgBox "Invalid Input - Not Within Limit", 16, "Invalid Input."
End If
End If
End If
Loop Until Len(InputvarTemp) > 0 And msgResult = vbYes And StrPtr(InputvarTemp) = 1 And IsNull(InputvarTemp) = False 'loop 1 check Empty / Null or Cancelled input
Loop Until CDec(InputvarTemp) >= 100 And CDec(InputvarTemp) <= 200 Or InputvarTemp = "Failed" 'loop 2 check within limit
Select Case InputvarTemp
Case "Failed"
MsgBox "Test Criteria Failed, Contact Production Engineer", 16, "Failed Test Criteria."
Case Else
MsgBox "Test Criteria Passed", 16, "Passed Test Criteria."
End Select
End Sub
Can anyone point me to the problem? Many thanks in advance. This is a part of a bigger code project but this part is not working I have isolated this code in to a single file to run by itself to figure out the issue.
To better understand what's going on, you need to write your code in such a way that it does as little as possible; right now you have a single procedure that does so many things it's hard to tell exactly what's going wrong and where.
Write a function to confirm user's valid numeric input:
Private Function ConfirmUserInput(ByVal input As Integer) As Boolean
ConfirmUserInput = MsgBox("Confirm value: " & CStr(input) & "?", vbYesNo) = vbYes
End Function
Then write a function to deal with user's input:
Private Function IsValidUserInput(ByVal userInput As String,_
ByVal lowerLimit As Double, _
ByVal upperLimit As Double) _
As Boolean
Dim result As Boolean
Dim numericInput As Double
If StrPtr(userInput) = 0 Then
'msgbox / cannot cancel out
ElseIf userInput = vbNullString Then
'msgbox / invalid empty input
ElseIf Not IsNumeric(userInput) Then
'msgbox / must be a number
Else
numericInput = CDbl(userInput)
If numericInput < lowerLimit Or numericInput > upperLimit Then
'msgbox / must be within range
Else
result = ConfirmUserInput(numericInput)
End If
End If
IsValidUserInput = result
End Function
This function can probably be written in a better way, but nonetheless it will return False if any of the validation rules fail, or if user doesn't confirm their valid input. Now you're equipped for looping, and since all the complex logic is extracted into its own function, the loop body gets pretty easy to follow:
Private Function GetTestCriteria(ByVal lowerLimit As Double, _
ByVal upperLimit As Double) As Boolean
Const failed As String = "Failed"
Dim prompt As String
prompt = "Enter Value between " & lowerLimit & _
" and " & upperLimit & " (Inclusive)."
Dim userInput As String
Dim isValid As Boolean
Do
userInput = InputBox(prompt, prompt)
isValid = IsValidUserInput(userInput, lowerLimit, upperLimit) _
Or userInput = failed
Loop Until IsValid
GetTestCriteria = (userInput <> failed)
End Sub
The OnSlideShowPageChange procedure can now look like this:
Private Sub OnSlideShowPageChange(ByVal SSW As SlideShowWindow)
If GetTestCriteria(100, 200) Then
MsgBox "Test criteria passed."
Else
MsgBox "Test criteria failed, contact production engineer."
End If
End Sub
I haven't tested any of this code, but I'm sure debugging these more specialized functions will be easier than debugging your monolithic chunk of code; by extracting these functions, you untangle the logic, and I bet the above does exactly what you're trying to do. Also note:
Dim xType, xLimitHi, xLimitLo, xPrompt As String declares xPrompt as a String, and everything else as a Variant. I don't think that's your intent here.
Select Case is best used with Enum values; use If-ElseIf constructs otherwise.
Slight modifications, per below comment:
how do i capture the user input to do something like write to a file
Now if you wanted to do something with the valid user inputs, say, write them to a file, you'd need GetTestCriteria to return the input - but that function is already returning a Boolean. One solution could be to use an "out" parameter:
Private Function GetTestCriteria(ByVal lowerLimit As Double, _
ByVal upperLimit As Double, _
ByRef outResult As Double) As Boolean
Const failed As String = "Failed"
Dim prompt As String
prompt = "Enter Value between " & lowerLimit & _
" and " & upperLimit & " (Inclusive)."
Dim userInput As String
Dim isValid As Boolean
Do
userInput = InputBox(prompt, prompt)
isValid = IsValidUserInput(userInput, lowerLimit, upperLimit, outResult) _
Or userInput = failed
Loop Until IsValid
GetTestCriteria = (userInput <> failed)
End Sub
Private Function IsValidUserInput(ByVal userInput As String,_
ByVal lowerLimit As Double, _
ByVal upperLimit As Double, _
ByRef outResult As Double) _
As Boolean
Dim result As Boolean
Dim numericInput As Double
If StrPtr(userInput) = 0 Then
'msgbox / cannot cancel out
ElseIf userInput = vbNullString Then
'msgbox / invalid empty input
ElseIf Not IsNumeric(userInput) Then
'msgbox / must be a number
Else
numericInput = CDbl(userInput)
If numericInput < lowerLimit Or numericInput > upperLimit Then
'msgbox / must be within range
Else
result = ConfirmUserInput(numericInput)
outResult = numericInput
End If
End If
IsValidUserInput = result
End Function
And now you can call a method in OnSlideShowPageChange, to write the valid result to a file:
Private Sub OnSlideShowPageChange(ByVal SSW As SlideShowWindow)
Dim result As Double
If GetTestCriteria(100, 200, result) Then
MsgBox "Test criteria passed."
WriteResultToFile result
Else
MsgBox "Test criteria failed, contact production engineer."
End If
End Sub
If you run into issues implementing this WriteResultToFile procedure, and existing Stack Overflow questions don't have an answer for you (slightly unlikely), feel free to ask another question!
Retailcoder's answer as a general approach is top notch. I would like to draw attention specifically to the use of IsNumeric() which would solve most issues. Currently your code fails if any non-numeric string is entered.
Had a look at the code to try and see if I could at least answer what was happening to try and appease your curiosity. You mentioned that it looked like you couldn't leave your second loop. In practice I was unable to exit your first loop. I'm sure was due to the StrPtr(InputvarTemp) = 1. I didn't even know what that was until I looked it up. In short it is an undocumented feature that was used to check if Cancel was pushed / get the underlying memory address of variables (apparently).
Before the end of the first loop I put this in for debugging
MsgBox Len(InputvarTemp) & " " & msgResult & " " & StrPtr(InputvarTemp) & " " & IsNull(InputvarTemp)
When I type "150" in the InputBox the results of the message box are as follows. The third value represent the StrPtr(InputvarTemp)
3 6 246501864 FALSE
246501864 is greater than 1 which would cause the loop exit to fail. Again, retailcoder has an excellent answer and I will not reinvent his wheel.
with thanks to #retailcoder and #Matt below is the completed code for any to use, your help is truly appropriated
Capture user input to a file(s) from Powerpoint presentation, using a Config.ini to minimize everyday programming (or no programming code to a standard user)
> Code in Slide 1
Option Explicit
Option Compare Text
Public WithEvents PPTEvent As Application
Public TimeNow, ToDate As String
Public WorkOrder, Serial, UserName As String
Public ReportFile, TempReportFile, TimingFile As String
Sub OnSlideShowPageChange(ByVal SSW As SlideShowWindow)
'Declare Variables
Dim ShellRun As Long
Dim INIPath, StartTime, EndTime, TimeDifferance As String ' from Enviorment
Dim PCPver, ModuleName, PCPFileName, Timed, ResultsFolder, TrainingFolder, TimeingFolder, TrainedFolder, xType, xPrompt, xvarUnit, y As String 'From INI file
Dim xLimitHi, xLimitLo As Variant
Dim result As Double
Dim FailedResult As Double
Dim PCPverInput, inputvar, InputvarDate, InputvarTrueFalse, InputvarGeneral, InputvarLimit, InputvarTemp As String 'From User
Dim TrainingFile, SelfCheck, InvalidCharacter1, InvalidCharacter2 As String 'Variables for Filenames
Dim msgResult, msgResultTemp As Integer
Dim myVarPass As Boolean
Dim KeyAscii As Integer 'Try and Hook Esc key
Dim ppApp As Object
Const fsoForAppend = 8
'Declare and create a FileSystemObject.
Dim fso, ResutlsFSO, TrainingFSO, TimeingFSO As Object 'Need Microsoft Script Runtime in references
' Declare a TextStream.
Dim oFile, ResutlsStream, TrainingStream, TimeingStream As Object
'Assign Variables
INIPath = ActivePresentation.Path & "\" & "Config.ini"
'ShellRun = Shell(ActivePresentation.Path & "\" & "Esc.exe")
SelfCheck = ActivePresentation.Name
ToDate = Format(Date, "dd-mmm-yyyy")
TimeNow = Replace(Format(time, "hh:mm:ss"), ":", "-")
StartTime = Format(time, "hh:mm:ss")
'Retrive Folderpaths and create file names
ModuleName = GetINIString("PCPInfo", "ModuleName", INIPath)
Timed = GetINIString("Options", "Timed", INIPath)
Set ResutlsFSO = CreateObject("Scripting.FileSystemObject")
Set TrainingFSO = CreateObject("Scripting.FileSystemObject")
Set TimeingFSO = CreateObject("Scripting.FileSystemObject")
'Retrive PCP version from Ini file
PCPver = GetINIString("PCPInfo", "PCPver", INIPath)
PCPFileName = GetINIString("PCPInfo", "PCPFileName", INIPath)
ResultsFolder = GetINIString("Folders", "ResultsFolder", INIPath)
TrainingFolder = GetINIString("Folders", "TrainingFolder", INIPath)
TimeingFolder = GetINIString("Folders", "TimeingFolder", INIPath)
TrainedFolder = GetINIString("Folders", "TrainedFolder", INIPath)
Do
If (SelfCheck <> PCPFileName) Then
MsgBox "Invalid Config.ini File. Replace with Correct INI file to continue. ", 16, "Invalid Config.ini File."
End If
Loop Until (SelfCheck = PCPFileName)
'Collect PCP version, User Name, Work Order, Serial Number
If (SSW.View.CurrentShowPosition = 1) Then
'Retrive PCP Version from BOM - User Input
Do
Do
PCPverInput = InputBox("Enter PCP Number including Version", "Enter PCP Number including Version")
If (Len(PCPverInput) < 4) Then
MsgBox "Invalid Input - PCP version cannot be Empty / Null / cancelled", vbOKOnly, "Invalid Input"
End If
Loop Until (Len(PCPverInput) > 4)
'Check PCPversion against BOM
If (PCPver <> PCPverInput) Then
'Display Warning Messages
MsgBox "Incorrect PCP version. Contact Team Leader / Product Engineer. Cannot Continue the programm", 16, "Incorrect PCP version."
End If
Loop Until (PCPver = PCPverInput)
'Retrive UserName - User Input
Do
msgResult = 7
Do
UserName = InputBox("Enter / Scan Operator Name", "Enter / Scan Operator Name")
msgResult = MsgBox("You have Enterd Operator Name " & UserName, vbYesNo + vbDefaultButton2, "Operator Name")
If (Len(UserName) < 4) Then
MsgBox "Invalid Input - User / Operator Name cannot be Empty / Null / cancelled", 16, "Invalid Input"
End If
Loop Until (Len(UserName) > 4) And (msgResult = vbYes)
Loop Until (Len(UserName) > 4)
'Retrive Work Order
Do
msgResult = 7
Do
WorkOrder = InputBox("Enter / Scan Work Order", "Enter / Scan Work Order")
msgResult = MsgBox("You have Enterd Work Order " & WorkOrder, vbYesNo + vbDefaultButton2, "Work Order")
If (Len(WorkOrder) < 4) Then
MsgBox "Invalid Input - Work Order cannot be Empty / Null / cancelled. Minimum 5 Numbers", 16, "Invalid Input"
End If
Loop Until (Len(WorkOrder) > 4) And (msgResult = vbYes)
Loop Until (Len(WorkOrder) > 4)
'Retrive Serial Number
Do
msgResult = 7
Do
Serial = InputBox("Enter / Scan Serial Number", "Enter / Scan Serial Number")
msgResult = MsgBox("You have Enterd Serial Number " & Serial, vbYesNo + vbDefaultButton2, "Serial Number")
If (Len(Serial) < 1) Then
MsgBox "Invalid Input - Serial Number cannot be Empty / Null / cancelled. Use -NOSERIAL- if Not Applicable", 16, "Invalid Input"
End If
Loop Until (Len(Serial) > 1) And (msgResult = vbYes)
Loop Until (Len(Serial) > 1)
If (Len(Dir(ResultsFolder, vbDirectory)) = 0) Then
MkDir ResultsFolder
End If
If (Len(Dir(ResultsFolder & "\" & WorkOrder, vbDirectory)) = 0) Then
MkDir ResultsFolder & "\" & WorkOrder
End If
If (Len(Dir(ResultsFolder & "\" & WorkOrder & "\" & Serial, vbDirectory)) = 0) Then
MkDir ResultsFolder & "\" & WorkOrder & "\" & Serial
End If
ReportFile = ResultsFolder & "\" & WorkOrder & "\" & Serial & "\" & PCPver & "_" & ToDate & "_" & TimeNow & ".txt"
Set ResutlsStream = ResutlsFSO.CreateTextFile(ReportFile, True)
ResutlsStream.WriteLine PCPver & " " & ModuleName & " Build / Test Checklist"
ResutlsStream.WriteLine "===================================================================================================="
ResutlsStream.WriteLine ""
ResutlsStream.WriteLine "Work Order :" & WorkOrder
ResutlsStream.WriteLine "Serial Number (if Applicable) :" & Serial
ResutlsStream.WriteLine "Test / Assembly Operator (Full Name) :" & UserName
ResutlsStream.WriteLine "Date (dd-mmm-yyyy) :" & ToDate
ResutlsStream.WriteLine ""
ResutlsStream.Close
If (Len(Dir(TrainingFolder, vbDirectory)) = 0) Then
MkDir TrainingFolder
End If
If (Len(Dir(TrainingFolder & "\" & UserName, vbDirectory)) = 0) Then
MkDir TrainingFolder & "\" & UserName
End If
TrainingFile = TrainingFolder & "\" & UserName & "\" & PCPver & ".csv"
If (Len(Dir(TrainingFile)) = 0) Then
Set TrainingStream = TrainingFSO.CreateTextFile(TrainingFile, True)
TrainingStream.WriteLine UserName & "'s " & ModuleName & " " & PCPver & " Training File"
TrainingStream.WriteLine "===================================================================================================="
TrainingStream.WriteLine "Operator" & Chr(44) & "PCP Version" & Chr(44) & "W/O" & Chr(44) & "Serial" & Chr(44) & "Date" & Chr(44) & "Time"
TrainingStream.WriteLine "===================================================================================================="
Else
Set TrainingStream = TrainingFSO.OpenTextFile(TrainingFile, 8)
End If
TrainingStream.WriteLine UserName & Chr(44) & PCPver & Chr(44) & WorkOrder & Chr(44) & Serial & Chr(44) & ToDate & Chr(44) & Format(time, "HH:MM:SS AM/PM")
TempReportFile = ReportFile
End If
'Detect Slide Number and Retrive Relevant Question from INI File
y = SSW.View.CurrentShowPosition
If (Len(y) > 0) Then
xType = GetINIString(SSW.View.CurrentShowPosition, "PromptType", INIPath)
If (Len(xType) > 0) Then
Set ResutlsStream = ResutlsFSO.OpenTextFile(TempReportFile, 8)
Select Case xType
Case "Message"
xPrompt = GetINIString(SSW.View.CurrentShowPosition, "Prompt", INIPath)
MsgBox xPrompt, vbYes, xPrompt
Case "Date"
xPrompt = GetINIString(SSW.View.CurrentShowPosition, "Prompt", INIPath)
Do
msgResult = 7
Do
inputvar = InputBox(xPrompt, "Enter Date")
InputvarDate = inputvar
msgResult = MsgBox("You have Enterd " & Format(inputvar, "dd-Mmm-yyyy") & " to " & xPrompt, vbYesNo + vbDefaultButton2, "Check Date Input")
If (StrPtr(inputvar) = 0) Or (Len(inputvar) < 6) Then
MsgBox "Invalid Date Input - Cannot be Empty / Null / cancelled. Enter a Valid date, in dd-Mmm-yyyy format", 16, "Invalid Input."
End If
inputvar = Format(inputvar, "dd-Mmm-yyyy")
If (Not IsDate(inputvar)) Then
MsgBox "Enter a Valid date, in dd-Mmm-yyyy format", 16, "Invalid Date."
End If
Loop Until (IsDate(inputvar) = True) And (msgResult = vbYes) And (Len(InputvarDate) > 6)
Loop Until (IsDate(inputvar) = True) And (msgResult = vbYes)
ResutlsStream.WriteLine "Step " & SSW.View.CurrentShowPosition & ". " & xPrompt & Chr(9) & ":" & Chr(9) & inputvar & " " & xvarUnit
Case "TrueFalse"
xPrompt = GetINIString(SSW.View.CurrentShowPosition, "Prompt", INIPath)
Do
msgResult = 7
Do
inputvar = InputBox(xPrompt, "Enter True or False")
msgResult = MsgBox("You have Enterd " & inputvar & " to " & xPrompt, vbYesNo + vbDefaultButton2, "Check Your Input (True/False)")
If (StrPtr(inputvar) = 0) Or (Len(inputvar) < 0) Then
MsgBox "Invalid Input - Cannot be Empty / Null / cancelled", 16, "Invalid Input."
End If
If (inputvar <> "True") And (inputvar <> "False") Then
MsgBox "Invalid Input - Enter Either True or False", 16, "Invalid Input."
End If
Loop Until (Len(inputvar) > 0) And (inputvar = "True") Or (inputvar = "False") And (msgResult = vbYes)
Loop Until (Len(inputvar) > 0) And (inputvar = "True") Or (inputvar = "False") And (msgResult = vbYes)
If inputvar = True Then
ResutlsStream.WriteLine "Step " & SSW.View.CurrentShowPosition & ". " & xPrompt & Chr(9) & ":" & Chr(9) & inputvar
Else
MsgBox "Test criteria failed, contact production engineer."
ResutlsStream.WriteLine "Step " & SSW.View.CurrentShowPosition & ". " & xPrompt & Chr(9) & ":" & Chr(9) & inputvar & " " & xvarUnit & " Failed" & " ***NCR Required***"
End If
Case "General"
xPrompt = GetINIString(SSW.View.CurrentShowPosition, "Prompt", INIPath)
Do
msgResult = 7
Do
inputvar = InputBox(xPrompt, xPrompt)
msgResult = MsgBox("You have Enterd " & inputvar & " to " & xPrompt, vbYesNo + vbDefaultButton2, "Check Input")
If (StrPtr(inputvar) = 0) Or (Len(inputvar) < 0) Then
MsgBox "Invalid Input - Cannot be Empty / Null / cancelled", 16, "Invalid Input."
End If
Loop Until (Len(inputvar) > 0) And (msgResult = vbYes)
Loop Until (Len(inputvar) > 0) And (msgResult = vbYes)
ResutlsStream.WriteLine "Step " & SSW.View.CurrentShowPosition & ". " & xPrompt & Chr(9) & ":" & Chr(9) & inputvar & " " & xvarUnit
Case "Limit"
xLimitHi = GetINIString(SSW.View.CurrentShowPosition, "LimitHi", INIPath)
xLimitLo = GetINIString(SSW.View.CurrentShowPosition, "LimitLo", INIPath)
xPrompt = GetINIString(SSW.View.CurrentShowPosition, "Prompt", INIPath)
xvarUnit = GetINIString(SSW.View.CurrentShowPosition, "varUnit", INIPath)
If GetTestCriteria(xPrompt, xLimitLo, xLimitHi, xvarUnit, result) Then
ResutlsStream.WriteLine "Step " & SSW.View.CurrentShowPosition & ". " & xPrompt & Chr(9) & ":" & Chr(9) & result & " " & xvarUnit
Else
MsgBox "Test criteria failed, contact production engineer."
Do
msgResult = 7
Do
FailedResult = InputBox("Enter Values Failed in " & xPrompt, "Enter Failed Value")
msgResult = MsgBox("You have Enterd Failed Value of " & FailedResult, vbYesNo + vbDefaultButton2, "Check Failed Input")
If (StrPtr(FailedResult) = 0) Or (Len(FailedResult) = 0) Then
MsgBox "Invalid Input - Cannot be Empty / Null / cancelled", 16, "Invalid Input."
End If
Loop Until (Len(FailedResult) > 0) And (msgResult = vbYes)
Loop Until (Len(FailedResult) > 0) And (msgResult = vbYes)
ResutlsStream.WriteLine "Step " & SSW.View.CurrentShowPosition & ". " & xPrompt & Chr(9) & ":" & Chr(9) & FailedResult & " " & xvarUnit & " Failed" & " ***NCR Required***"
End If
ResutlsStream.Close
End Select
End If
End If
If (Timed = "ON") Then
If (Len(Dir(TimeingFolder, vbDirectory)) = 0) Then
MkDir TimeingFolder
End If
If (Len(Dir(TimeingFolder & "\" & PCPver, vbDirectory)) = 0) Then
MkDir TimeingFolder & "\" & PCPver
End If
TimingFile = TimeingFolder & "\" & PCPver & "\" & "Timing-" & WorkOrder & "-" & Serial & "-" & PCPver & "-" & ToDate & ".csv"
If (Len(Dir(TimingFile)) = 0) Then
Set TimeingStream = TimeingFSO.CreateTextFile(TimingFile, True)
TimeingStream.WriteLine UserName & "'s " & ModuleName & " " & PCPver & " Build Time File"
TimeingStream.WriteLine "===================================================================================================="
TimeingStream.WriteLine "Seq/Step" & Chr(44) & "Start Time" & Chr(44) & "End Time"
Else
Set TimeingStream = TimeingFSO.OpenTextFile(TimingFile, 8)
End If
EndTime = Format(time, "hh:mm:ss")
TimeingStream.WriteLine "No:" & SSW.View.CurrentShowPosition & Chr(44) & StartTime & Chr(44) & EndTime
TimeingStream.Close
End If
End Sub
Private Function ConfirmUserInput(ByVal inputvar As Double) As Boolean
ConfirmUserInput = MsgBox("Confirm value: " & CStr(inputvar) & "?", vbYesNo + vbDefaultButton2, "Confirm value") = vbYes
End Function
Private Function IsValidUserInput(ByVal userInput As String, ByVal xLimitLo As Double, ByVal xLimitHi As Double, ByRef outResult As Double) As Boolean
Dim result As Boolean
Dim numericInput As Double
If StrPtr(userInput) = 0 Then
MsgBox "Invalid Input - Entry cannot be cancelled", 16, "Invalid User Input"
ElseIf userInput = vbNullString Then
MsgBox "Invalid Input - Entry cannot be Empty / Null", 16, "Invalid User Input"
ElseIf Not IsNumeric(userInput) Then
MsgBox "Invalid Input - Numeric Input required", 16, "Invalid User Input"
Else
numericInput = CDbl(userInput)
If numericInput < xLimitLo Or numericInput > xLimitHi Then
MsgBox "Invalid Input - Not within Limits", 16, "Invalid User Input"
Else
result = ConfirmUserInput(numericInput)
outResult = numericInput
End If
End If
IsValidUserInput = result
End Function
Private Function GetTestCriteria(ByVal xPrompt As String, ByVal xLimitLo As Double, ByVal xLimitHi As Double, ByVal xvarUnit As String, ByRef outResult As Double) As Boolean
Const failed As String = "Failed"
Dim prompt As String
prompt = "Enter Value between " & xLimitLo & xvarUnit & " and " & xLimitHi & xvarUnit & "(Inclusive)"
Dim userInput As String
Dim isValid As Boolean
Do
userInput = InputBox(prompt, xPrompt)
isValid = IsValidUserInput(userInput, xLimitLo, xLimitHi, outResult) Or userInput = failed
Loop Until isValid
GetTestCriteria = (userInput <> failed)
End Function
Private Sub TextBox1_Change()
End Sub
Private Sub TextBox2_Change()
End Sub
Private Sub TextBox2_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
End Sub
> Code in Module
Option Explicit
Option Compare Text
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
Private Const CONFIG_FILE = "Config.ini"
Public Function GetINIString(ByVal sApp As String, ByVal sKey As String, ByVal filepath As String) As String
Dim sBuf As String * 256
Dim lBuf As Long
lBuf = GetPrivateProfileString(sApp, sKey, "", sBuf, Len(sBuf), filepath)
GetINIString = Left$(sBuf, lBuf)
End Function
Public Function WriteINI(ByVal sApp As String, ByVal sKey As String, ByVal sValue As String) As String
WritePrivateProfileString sApp, sKey, sValue, "Config.ini"
End Function
Code in Config.ini
Config.ini to be remain in same folder as the .ppsm file
[PCPInfo]
;This will force the operator to check PCP version against BOM
;This is required as it is used to tie in the check list to the PCP
PCPver=12.3456.789.A01
;this is used as the heading for creating results files
ModuleName=NEW Validation Test Case
;this to check the correct PCP Power-point file is present with the ini file - if this is incorrect power point will not run
PCPFileName=12.3456.789.A01 NEW Validation Test Case.ppsm
[Options]
;Switch ON/OFF to collect timing data
Timed=ON
[Folders]
;If required creates last folder of the path
;folder where all check-lists/result files collected
ResultsFolder=C:\Reports\Validation
;folder where all training data collected
TrainingFolder=C:\Training Records
;folder where all timing data collected
TimeingFolder=C:\Times
;Check Who has completed training here - Not implemented
TrainedFolder=C:\TrainedOP
;Do not Use Slide No 1 - Use slide number in square brackets [x]
;First Slide collects Work Order, User name , Serial Number information
;PromptTypes Message,Date,TrueFalse,General,Limit *compulsory
;Type Message Displays Pop up message only , No Data Collection
;Type Date accepts dates in DD-MMM-YYYY format
;Type TrueFalse can be used for Passed failed, checks etc.
;Type General can be used for Part Serial numbers, batch dates
;Type Limit can be used for test parameters with a range,-
; - if not within the range "Failed" can be used to complete the step and return to a previous step
; LimitHi refers to Higher limit should be less than or equal to *compulsory for type Limit
; LimitLo Refers to Lower limit should be Greater than or equal to *compulsory for type Limit
;Prompt will pop-up the user input box wit the text as question/criteria *compulsory
;VarUnit Type of Unit Ohms,Psi,kPa etc.
[2]
PromptType=Message
LimitHi=
LimitLo=
Prompt=Revision Record
varUnit=
[4]
PromptType=Date
LimitHi=
LimitLo=
Prompt=Enter to days Date
varUnit=
[6]
PromptType=TrueFalse
LimitHi=
LimitLo=
Prompt=Enter True or False
varUnit=
[8]
PromptType=General
LimitHi=
LimitLo=
Prompt=Enter Any text
varUnit=
[10]
PromptType=Limit
LimitHi=200
LimitLo=100
Prompt=Enter Value within limits
varUnit=Bar
thanks again #retailcoder
best regards
Dumidu Roshan aka rellik - #rellik

Saving a table in VisualBasic 2010 to a .txt file

I am looking to get the table that is generated at the end of the program below have the option to be saved into a .txt so that it can be looked back to at a later date but am struggling to get this to happen.
Module Module1
Dim kTick As Integer
Dim kName(64) As String
Dim kHours(64) As Integer
Dim kHoursPay(64) As Integer
Dim kGross(64) As Integer
Dim kTax As Integer = 20
Dim kNet(64) As Integer
Dim kTotal As Integer
Dim kAverage As Integer
Sub Main()
'Assigning kTick (Number of employee's)
Assign()
'Imputting the data
Entry()
'Calculating kGross, kTax, kNet and kTotal
Print()
End Sub
Sub Assign()
Console.ForegroundColor = ConsoleColor.Gray
Console.WriteLine("Please enter the total amount of employee's:")
Console.ForegroundColor = ConsoleColor.White
kTick = Console.ReadLine()
Console.Clear()
End Sub
Sub Entry()
For kCounter = 1 To kTick
Console.ForegroundColor = ConsoleColor.DarkGray
Console.WriteLine("Employee " & kCounter)
Console.ForegroundColor = ConsoleColor.Gray
Console.WriteLine("Please enter the employee name below:")
Console.ForegroundColor = ConsoleColor.White
kName(kCounter) = Console.ReadLine()
Do
Console.ForegroundColor = ConsoleColor.Gray
Console.WriteLine("Please enter the employees total hours worked below:")
Console.ForegroundColor = ConsoleColor.White
kHours(kCounter) = Console.ReadLine()
Loop Until kHours(kCounter) >= 0 And kHours(kCounter) <= 60
Do
Console.ForegroundColor = ConsoleColor.Gray
Console.WriteLine("Please enter the employees Hourly Pay below:")
Console.ForegroundColor = ConsoleColor.White
kHoursPay(kCounter) = Console.ReadLine()
Loop Until kHoursPay(kCounter) >= 6 And kHoursPay(kCounter) <= 250
Console.Clear()
Next
End Sub
Sub Print()
For kCounter = 1 To kTick
kGross(kCounter) = kHours(kCounter) * kHoursPay(kCounter)
Next
For kCounter = 1 To kTick
kNet(kCounter) = (kGross(kCounter) / 10) * 8
Next
For kCounter = 1 To kTick
kTotal = kTotal + kHours(kCounter)
Next
kAverage = kTotal / kTick
Console.ForegroundColor = ConsoleColor.Gray
Console.WriteLine("Name" & vbTab & "Hours" & vbTab & "Hourly Rate" & vbTab & "Gross Pay" & vbTab & "Tax" & vbTab & "Net Pay")
Console.ForegroundColor = ConsoleColor.White
For kCounter = 1 To kTick
Console.WriteLine(kName(kCounter) & vbTab & kHours(kCounter) & vbTab & "£" & kHoursPay(kCounter) & vbTab & vbTab & "£" & kGross(kCounter) & vbTab & vbTab & kTax & "%" & vbTab & "£" & kNet(kCounter))
Next
Console.ForegroundColor = ConsoleColor.Gray
Console.WriteLine("Total hours worked: " & kTotal)
Console.WriteLine("Total average hours worked: " & kAverage)
Console.WriteLine("Total number of employees: " & kTick)
Console.ReadLine()
Save()
End Sub
Sub Save()
End Sub
End Module
I am trying to get the code in the Save subroutine any help will be appreciated!
Thanks Kai
One note: you should not Save from Print since both are unrelated by nature. So you may want to print without saving or save without printing to the console.
A simple approach is using the File class, for example by using File.WriteAllText(path) or File.WriteAllLines(path). Therefore you need to store the text you want to output(to the console and the file) somewhere. For example in a List(Of String) variable.
You are looking for StreamWriter class. It has Write and WriteLine methods that can help you. It works in a same way as Console.WriteLine you are using.