Replace values in (1 Viewer)

Uldis007

New member
Local time
Today, 02:18
Joined
Nov 17, 2021
Messages
22
Hi!

In the Access form I have a text box that by default displays the name of the system user who is logged in to the system. But the problem is that the username is displayed in the format "firstname.surname". I need to replace the "dot" with a "space" and the first letters of the name and surname in capital letters.

I use this to get a windows username

Option Compare Database
Option Explicit

Public Function GetUserName() As String

GetUserName = Environ("UserName")

End Function
 

Gasman

Enthusiastic Amateur
Local time
Today, 00:18
Joined
Sep 21, 2011
Messages
14,286
Use the Replace() function

No need to play around with the first letters if just checking name?

? "paul" = "PAUL"
True

? "paul" = "Paul"
True

If it is for cosmetic purposes, then look at Instr() and Left() functions as well.
I would search for the . and amend to uppercase, then use the replace.
 

Uldis007

New member
Local time
Today, 02:18
Joined
Nov 17, 2021
Messages
22
Use the Replace() function

No need to play around with the first letters if just checking name?

? "paul" = "PAUL"
True

? "paul" = "Paul"
True

If it is for cosmetic purposes, then look at Instr() and Left() functions as well.
I would search for the . and amend to uppercase, then use the replace.
Thanks for the reply!

I try this but it doesn't work, obviously the code is wrong

Code:
Private Sub FormReplaceValue()

Dim FormReplaceValue As Integer
FormReplaceValue = Replace(Forms!Izvēlne!Text3, ".", " ")

End Sub
 

Gasman

Enthusiastic Amateur
Local time
Today, 00:18
Joined
Sep 21, 2011
Messages
14,286
Thanks for the reply!

I try this but it doesn't work, obviously the code is wrong

Code:
Private Sub FormReplaceValue()

Dim FormReplaceValue As Integer
FormReplaceValue = Replace(Forms!Izvēlne!Text3, ".", " ")

End Sub
Well no, all a Replace does is replace characters with others, so still remains a string? :(

Most people would just use
Code:
Forms!Izvēlne!Text3 =Replace(Forms!Izvēlne!Text3, ".", " ")
If in the form then use the Me. prefix instead of Forms!

I would not create a whole sub for it either?

You could also use strConv() to get your Proper names.
 
Last edited:

isladogs

MVP / VIP
Local time
Today, 00:18
Joined
Jan 14, 2017
Messages
18,218
Summarising all the advice above, use:
Me.YourTextboxName=StrConv(Replace(Environ("username"),"."," "),vbProperCase)

Or a slightly shorter version
Me.YourTextboxName=StrConv(Replace(Environ("username"),"."," "),3)
 

Uldis007

New member
Local time
Today, 02:18
Joined
Nov 17, 2021
Messages
22
Summarising all the advice above, use:
Me.YourTextboxName=StrConv(Replace(Environ("username"),"."," "),vbProperCase)

Or a slightly shorter version
Me.YourTextboxName=StrConv(Replace(Environ("username"),"."," "),3)


I combined your solution with my own and set the default value of the text box to "GetUserName ()" and it works

Code:
Public Function GetUserName() As String

GetUserName = StrConv(Replace(Environ("username"), ".", " "), 3)

End Function

1637142391793.png



Thank you very much for your response!
You saved my day!
 

Users who are viewing this thread

Top Bottom