2014-10-28 49 views
-2

我有一个表中的字段由日期和字符串组成,例如04Jan_lole。我想在Visual Basic中,产生这一领域,VB.Net中的字符串日期MS Access 2013

日期+ “_” + theName

给我失配误差,所以我尝试的日期部分转换为字符串:

StrField = theDate.ToString( “DDMMM”)

给我invali d限定符错误

我该怎么做才能生成这种格式:04Jan_lole? 注意到!如果我想将Date定义为DateTime,它会给我提供错误Visual Basic不支持自动化类型。

+0

你在哪里看到*“invalid qualifier error”*?它在设计时还是运行时?哪行代码导致错误?错误的全文是什么?如何声明'StrField'?它是什么类型? – 2014-10-28 14:41:30

+0

哦,等等......这是VBA吗?我假设,基于示例代码和问题的标签,你在VB.NET中编写代码。如果您试图在VBA中运行此代码,那肯定会解释为什么它不起作用。 VBA和VB.NET是完全不同的语言。 – 2014-10-28 14:49:54

回答

2

从你的问题中不清楚你为什么会得到无效限定符错误。也许是因为你试图在VBA中使用VB.NET语法?但是,如果实际使用VB.NET,下面的代码应该为你工作:

Public Function FormatDateAndName(theDate As Date, theName As String) As String 
    Return theDate.ToString("ddMMMM") & theName 
End Function 

然后,你可以这样调用它:

Dim myDate As New Date(2004, 1, 1) 
Dim myName As String = "_lole" 
Dim dateAndName As String = FormatDateAndName(myDate, myName) 
Console.WriteLine(dateAndName) ' Outputs "04Jan_lole" 

在VBA(而非VB.NET ),你应该能够使用它像这样:

Public Function FormatDateAndName(theDate As Date, theName As String) As String 
    FormatDateAndName = Format(theDate, "ddMMMM") & theName 
End Function 

然后你就可以这样调用:

Dim result As String 
result = FormatDateAndName(#1/1/2004#, "_lole") 
MsgBox(result)