• 0

VB6 help


Question

Hey guys and gals.. just wondering if there was anyone out there kind enough to have a look at my dodgy coding and to help me make it easier for anyone apart from me to understand..

ill explain the purpose of the code...

this is a school project where we have been given a fictitious company to bring into the 21st century. the files that we have been given contain details on a cleaning schedule. the only hard part is coverting their time schedule into a readable format. currently they are operating on a letter system where the date of the service is given a letter.

for example:

A - Monday Week 1

X - Saturday Week 4

i have been given the task of converting the date letter into the format Date, Week, month

below is my code but it contains a lot of IF and THEN statements which kinda makes it look like a junior coded it. this will be handed in as a final assesment so any code to make it look as though i know what im doing would be helpful in getting me that higher grade :woot:

even some pieces of code will be welcomed...

hopefully someone can help me get rid of the day_code -> day_num or the Mnth_no -> Mnth_txt

thanks for your time

asimo

to get this to work all you need is a form with one button on it and name it Cmd_date. it prints the output to the form. (to be changed later)

'Code starts here

Dim day_code As String

Dim day_num As Integer

Dim week As Integer

Dim day As String

Private Sub Cmd_date_Click()

'get day code from the user

day_code = InputBox("please enter the letter code. The letter code needs to be between A and X and typed in uppercase", "enter day code", "A")

'Convert day code into a numerical value

If day_code = "A" Then day_num = 1

If day_code = "B" Then day_num = 2

If day_code = "C" Then day_num = 3

If day_code = "D" Then day_num = 4

If day_code = "E" Then day_num = 5

If day_code = "F" Then day_num = 6

If day_code = "G" Then day_num = 7

If day_code = "H" Then day_num = 8

If day_code = "I" Then day_num = 9

If day_code = "J" Then day_num = 10

If day_code = "K" Then day_num = 11

If day_code = "L" Then day_num = 12

If day_code = "M" Then day_num = 13

If day_code = "N" Then day_num = 14

If day_code = "O" Then day_num = 15

If day_code = "P" Then day_num = 16

If day_code = "Q" Then day_num = 17

If day_code = "R" Then day_num = 18

If day_code = "S" Then day_num = 19

If day_code = "T" Then day_num = 20

If day_code = "U" Then day_num = 21

If day_code = "V" Then day_num = 22

If day_code = "W" Then day_num = 23

If day_code = "X" Then day_num = 24

'get which week the day numerical value belongs to

If day_num < 6 Then week = 1

If day_num > 6 Then week = 2

If day_num > 12 Then week = 3

If day_num > 18 Then week = 4

'get the day

If week = 2 Then day_num = day_num - 6

If week = 3 Then day_num = day_num - 12

If week = 4 Then day_num = day_num - 18

If day_num = 1 Then day = "Monday"

If day_num = 2 Then day = "Tuesday"

If day_num = 3 Then day = "Wednesday"

If day_num = 4 Then day = "Thursday"

If day_num = 5 Then day = "Friday"

If day_num = 6 Then day = "Saturday"

'get the current date and convert it to the text equivalent

mnth_no = Month(Now)

If mnth_no = 1 Then Mnth_text = "January"

If mnth_no = 2 Then Mnth_text = "February"

If mnth_no = 3 Then Mnth_text = "March"

If mnth_no = 4 Then Mnth_text = "April"

If mnth_no = 5 Then Mnth_text = "May"

If mnth_no = 6 Then Mnth_text = "June"

If mnth_no = 7 Then Mnth_text = "July"

If mnth_no = 8 Then Mnth_text = "August"

If mnth_no = 9 Then Mnth_text = "September"

If mnth_no = 10 Then Mnth_text = "October"

If mnth_no = 11 Then Mnth_text = "November"

If mnth_no = 12 Then Mnth_text = "December"

'print the day, week and month to the form

Print day; ", Week "; week; ", "; Mnth_text

End Sub

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0

Here is the Day number:

day_num=Asc(UCase(day_code)) - 64

Here is a much simpler month conversion.

Mnth_text=Format(Date, "mmmm")

Also, your week code is broken (what if day_num=6).

If day_num < 7 Then

week = 1

ElseIf day_num < 13 Then

week = 2

ElseIf day_num < 19 Then

week = 3

ElseIf day_num < 25 Then

week = 4

End If

Link to comment
Share on other sites

  • 0

declare an array, fill the array with the letters of the alphabet...

Alpha = {A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X}

Now use a 'for' loop to incrementally check...

for A=1 to 24

      if day_code == Alpha[a] then Day_num = A

Use if else if, and start checking for GREATER THAN, while reducing the number...

Combine the two sets of if statements into one...

if Day_num &gt; 18 then
     Week = 4
     day_num -= 18
else if Day_num &gt; 12 then
      Week = 3
      day_num -= 12
else if Day_num &gt; 6 then
       Week = 2
       day_num -= 6
else
        Week = 1

Create an array of strings to hold the days of the week

Days = {Monday,Tudesday, Wednesday, Thursday, Friday,Saturday}

Now, use a 'for' loop to incrementally check

for B = 1 to 6

      if day_num == B then day = Days[B]

This code will use more resources, but it's cleaner and makes use of conditionals and loops... a little more appropriate. There are more efficient ways, but who cares...

Link to comment
Share on other sites

  • 0

yeah ascii is easy. so much easier than a huge, pointless array :p

you could also use a few functions to make it look nicer. That one sub is a bit big if you ask me.

Link to comment
Share on other sites

  • 0

YOU GUYS ROCK!!!

thanks for all your help

i never would have thought about using ascii to convert it back into a number..

now to implement and hand in.. thanks heapz

Link to comment
Share on other sites

  • 0

You could also get the week number in one step.

week=day_num/8 + 1

Of course we're assuming here that the 1st starts on the first day of the week instead of in the middle?

Link to comment
Share on other sites

  • 0

That would not be correct kjordan2001. In his project, there are only 6 days in a week. There is a simpler way to do it, but it is a small improvement.

Link to comment
Share on other sites

  • 0
That would not be correct kjordan2001. In his project, there are only 6 days in a week. There is a simpler way to do it, but it is a small improvement.

Alright then, day_num/6+1 or day_num/7+1....I guess...I'm not sure, this whole thing has me confused. I'm sure there's some way by dividing by some number. A lot quicker than a bunch of if statements, and a lot less code.

Edited by kjordan2001
Link to comment
Share on other sites

  • 0

week=(((day_num - 1) \ 6) + 1)

This is a single line to calculate it, but it was such a small gain that I didn't think it really matter (especially when compared to the day number assignment).

Glad to be of help asimo.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.