Access function to return user's name, not username - windows

Is there a function I can use to return the current users name to a textbox, rather than their username - so Joe Bloggs, not jbloggs?

Assuming you have Active Directory set up, you can use the following code, taken from Andrey Artemyev's answer here:
Public Function ADtest() As String
Dim ADSI As Object, UN As Object
Set ADSI = CreateObject("ADSystemInfo")
Set UN = GetObject("LDAP://" & ADSI.UserName)
ADtest = UN.FirstName
ADtest = ADtest & " " & UN.LastName
Set UN = Nothing
Set ADSI = Nothing
End Function
(Adding answer here for better visibility, made Community Wiki since it's not really my answer, and I don't want the credit)

Related

Unspecified error while opening a connection to database

Good eve guys.
I'm having a problem with my code.
The goal is, when I type a letter in the combobox, it wiill show a list of possible model names.
But whenever I type in that combobox, it gives me an error.
Here's the code I'm working on:
Private Sub cmbSearch_Change()
Dim conn As New ADODB.Connection
Dim record As New ADODB.Recordset
Dim model As String
model = cmbSearch.Text
If cmbSearch.Text <> "" Then
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & App.Path & "\Database.mdb"
cmbSearch.Clear
record.Open "SELECT Model FROM LaptopSpecs WHERE Model LIKE '" & model & "%' ORDER BY Model", conn, 3, 3
If record.RecordCount > 0 Then
Do While Not record.EOF
cmbSearch.AddItem record.Fields("Model").Value
record.MoveNext
Loop
End If
cmbSearch.Text = model
End If
Set record = Nothing
Set conn = Nothing
End Sub
In the part conn.open, the error message pops up.
run-time error '-2147467259(80004005)': unspecified error
Assuming you are using Access:
This question isn't about Access, but if it where:
Expanding on this answer, I recommend removing all the code in your example, and replacing it with this (done in Form design mode):
Set the cmbSearch.RowSource property to a fixed query like:
SELECT Model FROM LaptopSpecs ORDER BY Model
Set the cmbSearch Auto Expand to True.
This will work for 98% of all ComboBox type-to-select scenarios, and should work for your example.

How to translate a part using CATscript in CATIA?

I working with CATscript in CATIA to create Macros. I am trying to create a CATscript to translate a feature in CATIA.
When I run the CATscript I Should select the feature that should be translated and and the feature will be translated.
But I am getting an runtime error Type mismatch:'part1.CreateReferenceFromObject'
I could not find the solution for this problem.
Looking forward for your help.
Thanks in Advance.
Sub CATMain()
Set partDocument1 = CATIA.ActiveDocument
Set part1 = partDocument1.Part
Set hybridShapeFactory1 = part1.HybridShapeFactory
Set hybridShapeDirection1 = hybridShapeFactory1.AddNewDirectionByCoord(1.000000, 0.000000, 0.000000)
Set hybridShapeTranslate1 = hybridShapeFactory1.AddNewEmptyTranslate()
Set UserSel = partDocument1.Selection
Dim type1(0)
type1(0) = "HybridShape"
'--------------------------------------
'Dim input As Object
input = UserSel.SelectElement2(type1, "select input.", False)
Set reference1 = part1.CreateReferenceFromObject(input)
hybridShapeTranslate1.ElemToTranslate = reference1
hybridShapeTranslate1.Direction = hybridShapeDirection1
hybridShapeTranslate1.DistanceValue = 1.000000
Set hybridBody2 = hybridBodies1.Item("Geometrical Set.3")
hybridBody2.AppendHybridShape hybridShapeTranslate1
part1.InWorkObject = hybridShapeTranslate1
part1.Update
End Sub
your problem is that you are trying to create a reference from a Selection object.
input = UserSel.SelectElement2(type1, "select input.", False)
This returns the type Selection. You can dig into the input and get the actual object that you select.
try:
Dim myReference as Reference
Dim myExpectedObject as HybridShape 'or use variant
Set mySelectedObject = input.Item2(1).Value 'this will grab the first item from the selection collection
set myReference = part1.CreateReferenceFromObject(mySelectedObject)
'continue the rest of your code
Also, you should always clear the selection before you use a user selection as a good habit.
UserSel.Clear 'call this before you call a SelectElement selection function

get contact information for a particular email address

I am trying to write a script that would
go through a list of email addresses from my domain
for each of these emails addressess get the display name and job title
output the email, display name, job title to a csv file
I am sure this is fairly simply, the only problem that I have here is that I have no idea how to access the contact card of a contact.
How do I pass the strAddress variable to a olContactItem object?
EDIT:
To improve the question - I don't know how can I view the contact of the existing user#mydomain.com from a list of email addresses in a csv file (not added to my contacts list or anything)
The code I have so far:
Const olContactItem = 2
strEmail = "user#mydomain.com"
set fso = CreateObject("Scripting.FileSystemObject")
set appOutlook = CreateObject("Outlook.Application")
Set MyItem = appOutlook.CreateItem(olContactItem)
With MyItem
.Email1Address = strEmail
.jobTitle = strJobTitleVar
End With
I need to open the addess book page of that person, extract the values of Job Title and Display Name to relevant variables. However, I get stuck, because I get to a point where I am rather adding a new contact than viewing the existing person's info.
Is this more clear? How can I search through the address book for a particular person's info?
Answering my own question - this seems to be doing everything I need:
strInputEmail = objInputFile.ReadLine
set appOutlook = CreateObject("Outlook.Application")
set objNameSpace = appOutlook.GetNameSpace("MAPI")
set objContacts = objNameSpace.GetDefaultFolder(olFolderContacts)
Set objContact = objContacts.Items.Find("[IMAddress] = """ & strInputEmail & """")
strFullName = objContact.FullName
strJobTitle = objContact.JobTitle
strBusinessAddress = objContact.BusinessAddress
msgBox strFullName & strJobTitle & strBusinessAddress
It reads the inputfile, gets the email address as a variable, finds the person, gets the particular info as variables.
Thanks for help anyway!
Try something along the following lines. To see what the other properties and their DASL names are, look at the live GAL objects using OutlookSpy (I am its author): either click IAddrBook button and drill down the container hierarchy or click IMAPISession | QueryIdentity to see the current user or (if you have a message with a particular recipient) click IMessage, go to the GetRecipientTable table, double on the recipient.
Once you have the IMailUser window, select the property that you need and look at the DASL edit box.
Const olContactItem = 2
strEmail = "user#mydomain.com"
set fso = CreateObject("Scripting.FileSystemObject")
set appOutlook = CreateObject("Outlook.Application")
set NS = appOutlook.GetNamespace("MAPI")
NS.Logon
set Recip = NS.CreateRecipient(strEmail)
Recip.Resolve
set AE = Recip.AddressEntry
Set MyItem = appOutlook.CreateItem(olContactItem)
With MyItem
.Email1Address = strEmail
'read PR_TITLE property
.jobTitle = AE.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3A17001F")
End With

How can I create datareport using recordset in vb6?

Private Sub showreport_Click()
sql = "select * from student_record_database where"
sql=sql+ Grade='" & Combo1.Text & "' AND Meal='" & Combo11.Text & "'"
Set RES = CON.Execute(sql)
Set DataReport1.DataSource = RES
DataReport1.WindowState = vbMaximized
DataReport1.Show vbModal
End Sub
I am using this code as record set to create a data report.
My task is to choose options from various combo boxes and then display it's report so record set is needed there..
My question is that whether this code is sufficient to create data report???
I didn't set any properties of data environment or data report such as (connection - command - sql) because I am passing this record set directly to data report,then no need to fire any sql in properties of data environment.
But unfortunately it is not showing desired output
Please help me.
Try this one:
Private sub cmdprint_click()
Dim rs as new adodb.recordset
rs.open "SQL Query Statement Here",CON, adOpenDynamic, adLockOptimistic
set datareport1.datasource=rs
datareport1.show
end sub
Notes:
The data report datasouce should be cleared during design mode. (See properties on the datareport and set its datasource property to empty.) Ohhh...one more thing, please keep in mind that you should set also the datafield property for each textbox object inside the datareport corresponding to the datafield on your database during design time...
I am using this method for a long time and it works fine.
Try this.
To add a quite to a string, use a double quite.
Also you missed spelled the second Combo1 reference as Combo11
Private Sub showreport_Click()
sql = "select * from student_record_database where "
sql = sql & "Grade=""" & Combo1.Text & """ AND Meal=""" & Combo1.Text & """"
Set RES = CON.Execute(sql)
Set DataReport1.DataSource = RES
DataReport1.WindowState = vbMaximized
DataReport1.Show vbModal
End Sub

Get first record from WMI ExecQuery

I have a simple vbscript for retrieving the Windows version:
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colVersions = objWMI.ExecQuery("Select * from Win32_OperatingSystem")
For Each objVer in colVersions
ver = objVer.Version
Next
Is is possible to get the first record or do I have to loop over all records in the collection. All examples I've seen are with For Each construction. I receive Expected end of statement error when I try:
ver = colVersions[0].Version
It looks like the return value of ExecQuery is not a proper collection.
For Each objVer in colVersions
ver = objVer.Version
exit for
Next
On Windows Vista and later, you can use the ItemIndex method to get a collection item by its index:
ver = colVersions.ItemIndex(0).Version
On earlier Windows versions, there's no way to do this I'm afraid.
Set objWMI = GetObject("WinMgmts:{ImpersonationLevel=Impersonate}!\\.\Root\CIMV2")
Set objOS = objWMI.ExecQuery("SELECT * FROM Win32_OperatingSystem").ItemIndex(0)
msgBox objOS.Version
Edit for Explanation:
By adding .ItemIndex(0) to your original query, you are grabbing the first item in the collection. This will eliminate the need for a For/Each loop.

Resources