8 Replies - 1208 Views - Last Post: 24 October 2008 - 06:35 AM Rate Topic: -----

#1 jremio  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 19-October 08

Help in function that returns a string

Posted 19 October 2008 - 06:57 PM

Hi all,

I just started programming about 1 week ago. i would appreciate if anyone can tell me how to code a function in VB6 that returns a values depending on the string.

For example, if the parameter passed is a string containing the value #yyyy#, i want the function to return the year, eg 2008. And if the value of the parameter is #yyyy##mm# the function should return eg 200810. which is the year and month and so on. Can anyone suggest how am i going to do this?

I know I have to use something like:
Public Function ReadKeyword(ByVal keyword As String) As String
If keyword = "#yyyy#" Then
sYear = Year(Now)
End If

End Function



However, it differs. Because if the keyword is #yyyy##mm##dd# it should return eg 20081020 but i also need it to return accordingly if the keyword is #yyyy##dd##mm# it should return eg. 20082010.

Any suggestions? Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Help in function that returns a string

#2 thava  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,606
  • Joined: 17-April 07

Re: Help in function that returns a string

Posted 19 October 2008 - 11:10 PM

you don't need to write a new function for this vb already have a function name called

Format(value,"Formating String")

should return the string you required

here value is any value (datatype in vb6),
formatting string is any string
Was This Post Helpful? 0
  • +
  • -

#3 jremio  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 19-October 08

Re: Help in function that returns a string

Posted 19 October 2008 - 11:32 PM

View Postthava, on 19 Oct, 2008 - 11:10 PM, said:

you don't need to write a new function for this vb already have a function name called

Format(value,"Formating String")

should return the string you required

here value is any value (datatype in vb6),
formatting string is any string


Hmm thanks for the reply but i think you got me wrong. Basically the ReadKeyword method i mentioned in my previous post takes a string value e.g "#yyyy#" from an .ini file. I already figured how to read the string value from the .ini file and store it into a variable. Now i need to convert the string "#yyyy#" into the current year, which is 2008, using the ReadKeyword method.

E.g:
fileName = ReadKeyword("#yyyy#")

This line have to return a value of 2008.

Also, if the parameter changes,
fileName = ReadKeyword("#yyyy##mm#")

This line have to return a value of 200810.

Similarly,
fileName = ReadKeyword("#yyyy##mm##dd#")

This line have to return a value of 20081020.

The order does not have to be fixed, it can be:
#yyyy##mm##dd#
#yyyy##dd##mm#
#dd##mm##yyyy#

Which makes it very confusing for me.

Any help will be greatly appreciated, thanks! :blink:

This post has been edited by jremio: 19 October 2008 - 11:34 PM

Was This Post Helpful? 0
  • +
  • -

#4 jremio  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 19-October 08

Re: Help in function that returns a string

Posted 20 October 2008 - 12:36 AM

After trying a few methods I've got it working, but the code is kinda tedious.

Public Function ReadKeyword(ByVal str As String) As String

If InStr(str, "#yyyy#") <> 0 And InStr(str, "#dd#") <> 0 And InStr(str, "#mm#") <> 0 Then

	str = Year(Now) & Month(Now) & Day(Now)
	
	ReadKeyword = str
	
	Else
	
	If InStr(str, "#yyyy#") = 0 And InStr(str, "#dd#") <> 0 And InStr(str, "#mm#") <> 0 Then
	
		str = Month(Now) & Day(Now)
		ReadKeyword = str
		
		Else
		If InStr(str, "#yyyy#") <> 0 And InStr(str, "#dd#") = 0 And InStr(str, "#mm#") <> 0 Then
		
			str = Year(Now) & Month(Now)
			ReadKeyword = str
			Else
			If InStr(str, "#yyyy#") <> 0 And InStr(str, "#dd#") <> 0 And InStr(str, "#mm#") = 0 Then
			
				str = Year(Now) & Day(Now)
				ReadKeyword = str
				Else
				If InStr(str, "#yyyy#") <> 0 And InStr(str, "#dd#") = 0 And InStr(str, "#mm#") = 0 Then
				
					str = Year(Now)
					ReadKeyword = str
					Else
					If InStr(str, "#yyyy#") = 0 And InStr(str, "#dd#") <> 0 And InStr(str, "#mm#") = 0 Then
					
						str = Day(Now)
						ReadKeyword = str
						Else
						If InStr(str, "#yyyy#") = 0 And InStr(str, "#dd#") = 0 And InStr(str, "#mm#") <> 0 Then
							
							str = Month(Now)
							ReadKeyword = str
						End If
					End If
				End If
			End If
		End If
	End If
End If


End Function




Wonder if theres a simpler way to do this?
Was This Post Helpful? 0
  • +
  • -

#5 thava  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,606
  • Joined: 17-April 07

Re: Help in function that returns a string

Posted 20 October 2008 - 03:44 AM

if you ask me like that in your previous post i will explain you

now here is the coding
public Sub command1_click()
Dim keystr As String
keystr = "#yyyy##mm##dd#"
MsgBox FormatkeywordFucntion(keystr)
keystr = "#yyyy##dd##mm#"
MsgBox FormatkeywordFucntion(keystr)
keystr = "#dd##mm##yyyy#"
MsgBox FormatkeywordFucntion(keystr)
End Sub

Public Function FormatkeywordFucntion(keystr As String) As String
Dim Formatstr As String
Formatstr = Replace(keystr, "#", "")
FormatkeywordFucntion = Format(Now, Formatstr)
End Function



will you got it that's why i say the format function do the things that you want
if you want to thank me just click this post was helpful
Was This Post Helpful? 1
  • +
  • -

#6 jremio  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 19-October 08

Re: Help in function that returns a string

Posted 21 October 2008 - 02:42 AM

View Postthava, on 20 Oct, 2008 - 03:44 AM, said:

if you ask me like that in your previous post i will explain you

now here is the coding
public Sub command1_click()
Dim keystr As String
keystr = "#yyyy##mm##dd#"
MsgBox FormatkeywordFucntion(keystr)
keystr = "#yyyy##dd##mm#"
MsgBox FormatkeywordFucntion(keystr)
keystr = "#dd##mm##yyyy#"
MsgBox FormatkeywordFucntion(keystr)
End Sub

Public Function FormatkeywordFucntion(keystr As String) As String
Dim Formatstr As String
Formatstr = Replace(keystr, "#", "")
FormatkeywordFucntion = Format(Now, Formatstr)
End Function



will you got it that's why i say the format function do the things that you want
if you want to thank me just click this post was helpful




:^: Your method works! Thanks for the effort. However I am using this now [feels easier for me :D ]:

Public Function ReadKeyword(str As String) As String
str = Replace(str, "#yyyy#", Year(Now))
str = Replace(str, "#mm#", Month(Now))
str = Replace(str, "#dd#", Day(Now))
str = Replace(str, "#hh#", Hour(Now))
str = Replace(str, "#nn#", Minute(Now))
str = Replace(str, "#today#", Year(Now) & Month(Now) & Day(Now))
str = Replace(str, "#yesterday#", Year(Now) & Month(Now) & Day(Now) - 1)

ReadKeyword = str

End Function


Was This Post Helpful? 0
  • +
  • -

#7 akhileshbc  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 179
  • Joined: 27-September 08

Re: Help in function that returns a string

Posted 21 October 2008 - 05:54 AM

Both methods are nice... I like it.. :)
Was This Post Helpful? 0
  • +
  • -

#8 eyesice  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 23-October 08

Re: Help in function that returns a string

Posted 24 October 2008 - 01:41 AM

Try modifying this to suit your need.
format(now, replace("#yyyy##mm##dd#", "#", "",1,,vbTextCompare))

Was This Post Helpful? 0
  • +
  • -

#9 Reverand Dave  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • Posts: 375
  • Joined: 27-July 08

Re: Help in function that returns a string

Posted 24 October 2008 - 06:35 AM

View Postjremio, on 21 Oct, 2008 - 02:42 AM, said:

Public Function ReadKeyword(str As String) As String
str = Replace(str, "#yyyy#", Year(Now))
str = Replace(str, "#mm#", Month(Now))
str = Replace(str, "#dd#", Day(Now))
str = Replace(str, "#hh#", Hour(Now))
str = Replace(str, "#nn#", Minute(Now))
str = Replace(str, "#today#", Year(Now) & Month(Now) & Day(Now))
str = Replace(str, "#yesterday#", Year(Now) & Month(Now) & Day(Now) - 1)

ReadKeyword = str

End Function




I like this alot! :^: :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1